0

解决这个问题可能很简单,但我目前似乎想不出更简单的解决方案。我正在使用 Excel.js 并希望将数组中的值添加到一组特定的单元格中。在这个例子中E6:E25

我面临的问题是我似乎无法选择特定的单元格,只能在一列中选择所有单元格。

var array = ["4","3","3","3","3","8","2","4","5","2","2","6","4","3","8","5","4","3","2","2"]

workbook.xlsx.readFile('myFile.xlsx')
  .then(function() {
    var worksheet = workbook.getWorksheet(1);
    var column = worksheet.getColumn(5) // Selecting specific column
    column.eachCell(function(cell, rownumber){
      // eachCell count is 122, ideally not wanting to iterate 122 times
      // when only needing 20
      switch (cell['_address']) {
        case 'E6':
          worksheet.getCell(cell['_address']).value = array[0];
          break;
        case 'E7':
          worksheet.getCell(cell['_address']).value = array[1];
          break;
        case 'E8':
          worksheet.getCell(cell['_address']).value = array[2];
          break;
          // and so on 20 times
        default:
      }

    });

    return workbook.xlsx.writeFile('newFile.xlsx');
})

我知道这太可怕了;我希望对其进行重构,使其更加清洁和高效。

4

1 回答 1

1

像这样的简单 for 循环不做同样的事情吗?

var array = ["4","3","3","3","3","8","2","4","5","2","2","6","4","3","8","5","4","3","2","2"];

for (i=0; i<array.length; i++) {
    col = i+6;
    worksheet.getCell('E'+col).value = array[i];
}
于 2018-02-05T21:06:56.083 回答