获取单元格

  1. const cell = worksheet.getCell('C3'); //通过单元格行号列标获取
  2. const cell = worksheet.getColumn(3).values[2]; //通过列获取
  3. const cell = worksheet.getRow(3).values[3]; //通过行获取

设置值

  1. cell.value = new Date(1968, 5, 1); // 修改/添加单个单元格
  2. expect(cell.type).toEqual(Excel.ValueType.Date); // 查询单元格的类型
  3. myInput.value = cell.text; // 使用单元格的字符串值
  4. const html = '<div>' + cell.html + '</div>'; // 使用 html 安全的字符串进行渲染...

自动换行

  1. worksheet.getCell("A1").alignment = {wrapText: true} // 自动换行

合并单元格 mergeCells

  1. worksheet.mergeCells('A4:B5'); // 合并一系列单元格
  2. // 按左上,右下合并
  3. worksheet.mergeCells('K10', 'M12');
  4. // 按开始行,开始列,结束行,结束列合并(相当于 K10:M12)
  5. worksheet.mergeCells(10,11,12,13);
  6. // ...合并的单元格被链接起来了
  7. worksheet.getCell('B5').value = 'Hello, World!';
  8. expect(worksheet.getCell('B5').value).toBe(worksheet.getCell('A4').value);
  9. expect(worksheet.getCell('B5').master).toBe(worksheet.getCell('A4'));
  10. // ...合并的单元格共享相同的样式对象
  11. expect(worksheet.getCell('B5').style).toBe(worksheet.getCell('A4').style);
  12. worksheet.getCell('B5').style.font = myFonts.arial;
  13. expect(worksheet.getCell('A4').style.font).toBe(myFonts.arial);
  14. // 取消单元格合并将打破链接的样式
  15. worksheet.unMergeCells('A4');
  16. expect(worksheet.getCell('B5').style).not.toBe(worksheet.getCell('A4').style);
  17. expect(worksheet.getCell('B5').style.font).not.toBe(myFonts.arial);

定义名称 name

  1. // 为单元格分配(或获取)名称(将覆盖该单元具有的其他任何名称)
  2. worksheet.getCell('A1').name = 'PI';
  3. expect(worksheet.getCell('A1').name).to.equal('PI');
  4. // 为单元格分配(或获取)一组名称(单元可以具有多个名称)
  5. worksheet.getCell('A1').names = ['thing1', 'thing2'];
  6. expect(worksheet.getCell('A1').names).to.have.members(['thing1', 'thing2']);
  7. // 从单元格中删除名称
  8. worksheet.getCell('A1').removeName('thing1');
  9. expect(worksheet.getCell('A1').names).to.have.members(['thing2']);

数据有效性

image.png image.png image.png

  1. worksheet.getCell('A1').dataValidation = {
  2. //设置
  3. type: 'list', //有效条件,也就是上图的 允许(A)。其他类型见下
  4. allowBlank: true, //忽略空值
  5. operator:'', //数据
  6. formulae: ['"One,Two,Three,Four"'], //来源,可以多个单元格的值,比如以D5 到F5的值 formulae: ['$D$5:$F$5'],比如日期formulae: [new Date(2016,0,1)]
  7. //输入信息
  8. showInputMessage:true, //选定单元格时显示输入信息
  9. promptTitle:'123', //输入信息标题
  10. prompt:'abc', //输入信息
  11. //出错警告
  12. showErrorMessage: true, //输入无效数据时显示出错警告
  13. errorStyle: 'error', //样式,"warning"为警告,"information"为信息
  14. errorTitle: '123', //错误信息的标题
  15. error: 'abc' //错误信息
  16. };

type类型:

类型 描述
list 下拉列表
whole 该值必须是整数
date 日期
time 时间
textLength 该值可以是文本,但长度是受控的
custom 自定义公式控制有效值

数据operator:
对于 list 或 custom 以外的其他类型,可以输入下面的运算符,来表示要求用户输入的数值的范围

运算符 描述
between 介于
notBetween 未介于
equal 等于
notEqual 不等于
greaterThan 大于
lessThan 小于
greaterThanOrEqual 大于或等于
lessThanOrEqual 小于或等于

批注 note

  1. // 纯文字笔记
  2. worksheet.getCell('A1').note = 'Hello, ExcelJS!';
  3. // 彩色格式化的笔记
  4. worksheet.getCell('B1').note = {
  5. //批注的文本内容
  6. texts: [
  7. {'font': {'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'This is '},
  8. {'font': {'italic': true, 'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'a'},
  9. {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' '},
  10. {'font': {'size': 12, 'color': {'argb': 'FFFF6600'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'colorful'},
  11. {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' text '},
  12. {'font': {'size': 12, 'color': {'argb': 'FFCCFFCC'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'with'},
  13. {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' in-cell '},
  14. {'font': {'bold': true, 'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'format'},
  15. ]
  16. };