简体   繁体   中英

How to fix the Error : TypeError : Cannot read properly 'setValues' of undefined google app script. How to implement it to my the code?

I made files with table. On those table there is some datas that are changing according to each files. So I created a file where there is a table that sum all of the others tables that are in others files. First i made my code that sum tables of two files:

function juneFunction() {
  var sheet_origin1=SpreadsheetApp
                      .openById("ID1")
                      .getSheetByName("Calcul/Machine");
  var sheet_origin2=SpreadsheetApp
                      .openById("ID2")
                      .getSheetByName("Calcul/Machine"); 
  var sheet_destination=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //define range as a string in A1 notation
  var range1="BU4:CF71"  
  var values1=sheet_origin1.getRange(range1).getValues();
  var values2=sheet_origin2.getRange(range1).getValues(); 
  var destinationrange=sheet_destination.getRange("C3:N70");
  //Here you sum the values of equivalent cells from different sheets
  for(var i=0; i<values1.length;i++)
  {
    for(var j=0; j<values1[0].length;j++)
    {
      sum=values1[i][j]+values2[i][j];
      destinationrange.getCell(i+1,j+1).setValue(sum);
    }
  } 
}

This code is working perfectly. But my goal is to sum 26 tables of 26 files. So I tried to do it with 3 files, here is the code:

function juneFunction() {

  var sheet_origin1=SpreadsheetApp
                      .openById("ID1")
                      .getSheetByName("Calcul/Machine");
  var sheet_origin2=SpreadsheetApp
                      .openById("ID2")
                      .getSheetByName("Calcul/Machine");
  var sheet_origin3=SpreadsheetApp
                      .openById("ID3")
                      .getSheetByName("Calcul/Machine");
  var sheet_destination=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  
  //define range as a string in A1 notation
  var range1="BU4:CF71"  
  
  var values1=sheet_origin1.getRange(range1).getValues();
  var values2=sheet_origin2.getRange(range1).getValues();
  var values3=sheet_origin3.getRange(range1).getValues();
  
  var destinationrange=sheet_destination.getRange("C3:N70");
  
  //Here you sum the values of equivalent cells from different sheets
  for(var i=0; i<values1.length;i++)
  {
    for(var j=0; j<values1[0].length;j++)
    {
      for(var k=0; k<values1[0].length;k++){
        sum=values1[i][j][k]+values2[i][j][k]+values3[i][j][k];
        destinationrange.getCell[[i+1,j+1,k+1]].setValues(sum);
      }
    }
  }
}

Here I have this error: TypeError: Cannot read properly 'setValues' of undefined. This error happen here:

destinationrange.getCell[[i+1,j+1,k+1]].setValues(sum);

I think that the error is coming from the.getCell but i dont know why...

Range.getCell() takes two arguments: the row offset and the column offset. You are handing it just one argument: an array of three numbers.

You should not be using Range.getCell() in the first place. Instead, use Array.map() to get the sheets, Array.forEach() to iterate over them, and finally Range.setValues() to write all results in one go, like this:

function sumRangeAcrossManySpreadsheets() {
  const spreadSheetIds = ['id1', 'id2', 'id3',];
  const sheetName = 'Calcul/Machine';
  const rangeA1 = 'BU4:CF71';
  const targetRange = SpreadsheetApp.getActiveSheet().getRange('C3');
  const sheets = spreadSheetIds.map(id => SpreadsheetApp.openById(id).getSheetByName(sheetName));
  const result = [];
  sheets.forEach((sheet, sheetIndex) => {
    if (!sheet) {
      throw new Error(`There is no sheet ${sheetName} in spreadsheet ID '${spreadSheetIds[sheetIndex]}'.`);
    }
    const values = sheet.getRange(rangeA1).getValues();
    values.forEach((row, rowIndex) => row.forEach((value, columnIndex) => {
      if (!result[rowIndex]) {
        result[rowIndex] = new Array(row.length).fill(0);
      }
      result[rowIndex][columnIndex] += Number(value) || 0;
    }));
  });
  targetRange.offset(0, 0, result.length, result[0].length).setValues(result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM