获取单元格
const cell = worksheet.getCell('C3'); //通过单元格行号列标获取
const cell = worksheet.getColumn(3).values[2]; //通过列获取
const cell = worksheet.getRow(3).values[3]; //通过行获取
设置值
cell.value = new Date(1968, 5, 1); // 修改/添加单个单元格
expect(cell.type).toEqual(Excel.ValueType.Date); // 查询单元格的类型
myInput.value = cell.text; // 使用单元格的字符串值
const html = '<div>' + cell.html + '</div>'; // 使用 html 安全的字符串进行渲染...
自动换行
worksheet.getCell("A1").alignment = {wrapText: true} // 自动换行
合并单元格 mergeCells
worksheet.mergeCells('A4:B5'); // 合并一系列单元格
// 按左上,右下合并
worksheet.mergeCells('K10', 'M12');
// 按开始行,开始列,结束行,结束列合并(相当于 K10:M12)
worksheet.mergeCells(10,11,12,13);
// ...合并的单元格被链接起来了
worksheet.getCell('B5').value = 'Hello, World!';
expect(worksheet.getCell('B5').value).toBe(worksheet.getCell('A4').value);
expect(worksheet.getCell('B5').master).toBe(worksheet.getCell('A4'));
// ...合并的单元格共享相同的样式对象
expect(worksheet.getCell('B5').style).toBe(worksheet.getCell('A4').style);
worksheet.getCell('B5').style.font = myFonts.arial;
expect(worksheet.getCell('A4').style.font).toBe(myFonts.arial);
// 取消单元格合并将打破链接的样式
worksheet.unMergeCells('A4');
expect(worksheet.getCell('B5').style).not.toBe(worksheet.getCell('A4').style);
expect(worksheet.getCell('B5').style.font).not.toBe(myFonts.arial);
定义名称 name
// 为单元格分配(或获取)名称(将覆盖该单元具有的其他任何名称)
worksheet.getCell('A1').name = 'PI';
expect(worksheet.getCell('A1').name).to.equal('PI');
// 为单元格分配(或获取)一组名称(单元可以具有多个名称)
worksheet.getCell('A1').names = ['thing1', 'thing2'];
expect(worksheet.getCell('A1').names).to.have.members(['thing1', 'thing2']);
// 从单元格中删除名称
worksheet.getCell('A1').removeName('thing1');
expect(worksheet.getCell('A1').names).to.have.members(['thing2']);
数据有效性
worksheet.getCell('A1').dataValidation = {
//设置
type: 'list', //有效条件,也就是上图的 允许(A)。其他类型见下
allowBlank: true, //忽略空值
operator:'', //数据
formulae: ['"One,Two,Three,Four"'], //来源,可以多个单元格的值,比如以D5 到F5的值 formulae: ['$D$5:$F$5'],比如日期formulae: [new Date(2016,0,1)]
//输入信息
showInputMessage:true, //选定单元格时显示输入信息
promptTitle:'123', //输入信息标题
prompt:'abc', //输入信息
//出错警告
showErrorMessage: true, //输入无效数据时显示出错警告
errorStyle: 'error', //样式,"warning"为警告,"information"为信息
errorTitle: '123', //错误信息的标题
error: 'abc' //错误信息
};
type类型:
类型 | 描述 |
---|---|
list | 下拉列表 |
whole | 该值必须是整数 |
date | 日期 |
time | 时间 |
textLength | 该值可以是文本,但长度是受控的 |
custom | 自定义公式控制有效值 |
数据operator:
对于 list 或 custom 以外的其他类型,可以输入下面的运算符,来表示要求用户输入的数值的范围
运算符 | 描述 |
---|---|
between | 介于 |
notBetween | 未介于 |
equal | 等于 |
notEqual | 不等于 |
greaterThan | 大于 |
lessThan | 小于 |
greaterThanOrEqual | 大于或等于 |
lessThanOrEqual | 小于或等于 |
批注 note
// 纯文字笔记
worksheet.getCell('A1').note = 'Hello, ExcelJS!';
// 彩色格式化的笔记
worksheet.getCell('B1').note = {
//批注的文本内容
texts: [
{'font': {'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'This is '},
{'font': {'italic': true, 'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'a'},
{'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' '},
{'font': {'size': 12, 'color': {'argb': 'FFFF6600'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'colorful'},
{'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' text '},
{'font': {'size': 12, 'color': {'argb': 'FFCCFFCC'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'with'},
{'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' in-cell '},
{'font': {'bold': true, 'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'format'},
]
};