0

I want to receive the Excel data and put the value in the Excel column as a loop statement.

I want to put it in with the code below, what should I do?


//read file
const workbook = XLSX.readFile("test.xlsx");

//put value
workbook.Sheets.Sheet1.B2.v = 2000
workbook.Sheets.Sheet1.B3.v = 3000
workbook.Sheets.Sheet1.B4.v = 4000
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). Also see [JavaScript property access: dot notation vs. brackets?](/q/4968406/4642212). – Sebastian Simon Sep 26 '21 at 09:43
  • workbook.Sheets.Sheet1={ A1: { t: 's', v: 'a', r: 'a', h: 'a', w: 'a' }, B1: { t: 's', v: 'b', r: 'b', h: 'b', w: 'b' }, A2: { t: 'n', v: 0, w: '0' }, B2: { t: 'n', v: 2000, w: '0' }, A3: { t: 'n', v: 0, w: '0' }, B3: { t: 'n', v: 3000, w: '0' }, A4: { t: 'n', v: 0, w: '0' }, B4: { t: 'n', v: 4000, w: '0' } } – 한명훈 Sep 26 '21 at 09:54

1 Answers1

0

You can use bracket notations along with a for loop to do this.

With [] notation, you can use a variable to access a property of an object.

Initialize with a suitable value and run up til some limit. Here is sample code:

const workbook = XLSX.readFile("test.xlsx");
let cellsLimit = 100;
for(let i = 1 ; i <= cellsLimit; i++){
let keyName = 'B' + i;
workbook.Sheets.Sheet1[keyName].v = 1000 + (i*1000)
}
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39