alasql에서 내 보낸 Excel 수식이 작동하지 않습니다.

Dec 23 2020

내 HTML 페이지에서 아래 코드를 사용하여 Excel 파일을 만들고 다운로드합니다. 많은 형식으로이 동적 파일을 만들고 있습니다.

let db = new alasql.Database('TEMPDB');
db.exec('create table tempexcel(A string,B string, c string)');
db.exec('insert into tempexcel("5","6","=A1+B1")');
db.exec('select * INTO XLSX("tempex.xlsx",{headers:false})  from tempexcel');

이 코드는 Excel을 생성하는 데 잘 작동합니다. 그러나 C1 셀의 Excel 내용을 열면 = A1 + B1 Enter 키를 누르면 값이 평가됩니다. 모든 셀에 대해이 값을 평가하고 싶습니다. Excel 또는 alasql API에서 무언가를 변경해야하나요?

답변

1 RobinMackenzie Dec 23 2020 at 20:12

에 대한:

db.exec('create table tempexcel(A string,B string, c string)');

검사 데이터 유형을 alasql에 대한 것은 예를 들면 위해 아무것도 없다 xlfunction그래서 string열위한 최선의 방법이다 c.

따라서 문제는 Excel 통합 문서 생성을 위해 xlsx라는 라이브러리를 활용하는 alasql 자체 내에 있어야합니다. prepareSheet 함수에서 여기를 참조하십시오 .

for (var j = 0; j < dataLength; j++) {
    columns.forEach(function(col, idx) {
        var cell = {v: data[j][col.columnid]};
        if (typeof data[j][col.columnid] == 'number') {
            cell.t = 'n';
        } else if (typeof data[j][col.columnid] == 'string') {
            cell.t = 's';
        } else if (typeof data[j][col.columnid] == 'boolean') {
            cell.t = 'b';
        } else if (typeof data[j][col.columnid] == 'object') {
            if (data[j][col.columnid] instanceof Date) {
                cell.t = 'd';
            }
        }
        cells[alasql.utils.xlsnc(col0 + idx) + '' + i] = cell;
    });
    i++;
}

셀이 수식으로 표시되어야하는지 확인할 것이 없으며 숫자, 문자열, 부울 및 날짜 만 고려합니다 (데이터 유형 문서와 합리적으로 일치 함).

XLSX 라이브러리에서 셀을 수식으로 플래그를 지정하는 것은 간단합니다 . 그래서 우리는 그것을 alasql 코드에 적용 할 수 있습니다.

for (var j = 0; j < dataLength; j++) {
    columns.forEach(function (col, idx) {
        var isFormula = false; 
        var d = data[j][col.columnid];
        var cell;
        if (typeof d == 'string') {
            isFormula = d.substr(0, 1) == '=';
        }
        if (!isFormula) {
            cell = {v: data[j][col.columnid]};
            if (typeof data[j][col.columnid] == 'number') {
                cell.t = 'n';
            } else if (typeof data[j][col.columnid] == 'string') {
                cell.t = 's';
            } else if (typeof data[j][col.columnid] == 'boolean') {
                cell.t = 'b';
            } else if (typeof data[j][col.columnid] == 'object') {
                if (data[j][col.columnid] instanceof Date) {
                    cell.t = 'd';
                }
            }   
        } else {
            cell = {f: d.substr(1, d.length - 1)};
        }           
        cells[alasql.utils.xlsnc(col0 + idx) + '' + i] = cell;
    });
    i++;
}

값이 문자열이고 =다음으로 시작하는 경우 XLSX에 Excel이 수식임을 인식하는 방식으로 출력하도록 지시합니다 (그리고 =. 그렇지 않으면 alasql이 이미 수행하고있는 작업을 수행하십시오. 그건 그렇고 테스트되지 않고 제대로 구현되지 않은 해킹이지만 IMHO는 귀하의 질문에 대한 답변입니다.

alasql.fs.jsnode_modules 의 파일에 해킹하면 원래 코드가 예상대로 작동합니다.

나는 이것에 대해 alasql 프로젝트에서 문제 를 제기 할 자유를 얻었습니다 .