0
我應該如何將條件格式添加到我在Node.js中生成的Excel文件中的某些列中?當我將xml添加到工作表中像這樣answer時,我在Excel中收到錯誤,認爲文件打開。我產生Node.js的使用xlsx的文件,使Excel文件看起來像這裏面:如何在Javascript中添加條件格式到Excel文件?
- Sample.xlsx
- [CONTENT_TYPES] .XML
- ...
- XL
- _rels
- sharedString.xml
- styles.xml
- 主題
- workbook.xml
- 工作表
- sheet1.xml
我寫我的條件格式到工作表Sheet1 .xml,他重新是這樣做的代碼:
var fs = require("fs");
var jszip = require("jszip");
var CONDITIONAL_FORMATTING = '<conditionalFormatting sqref="A1">' +
'<cfRule type="expression" dxfId="0" priority="1"><formula>LEN(A1)&' +
'gt;2</formula></cfRule></conditionalFormatting>';
var WORKSHEET1 = 'xl/worksheets/sheet1.xml';
fs.readFile("excel/rewrite.xlsx", function(err, data) {
jszip.loadAsync(data).
then(function(zip) {
rezip = zip;
return zip.file(WORKSHEET1).async("string");
}).then(function (worksheet) {
var pos = worksheet.indexOf('<pageMargins');
if(pos > 0) {
worksheet = worksheet.substring(0,pos) +
CONDITIONAL_FORMATTING + worksheet.substring(pos);
}
rezip.file(WORKSHEET1,worksheet);
rezip.generateNodeStream({type:'nodebuffer',streamFiles:true})
.pipe(fs.createWriteStream('out.xlsx'))
.on('finish', function() {
console.log('done');
});
});
});