Home > front end >  Manage sheet range length on Google sheet
Manage sheet range length on Google sheet

Time:03-12

I am having a "Cannot read property 'length'" error on this code that I do not understand. For me newRangeVals[0].length should get the length of the sheet.

The code is created, for each file of a folder, to get data from an URL and to only keep data contained in the title

Does someone has an advice on this ?

  var name = spreadsheet.getName();
  let range = sheet.getDataRange(),
      maxRows = sheet.getMaxRows(),
      srchCol_1 = 2,
      srchPatt_1 = new RegExp(name)
      newRangeVals = range.getValues().filter(r => r[0] && srchPatt_1.exec(r[srchCol_1])),   
      numRows = newRangeVals.length;  
  range.clearContent();
  sheet.getRange(2,1, numRows, newRangeVals[0].length).setValues(newRangeVals);
  sheet.deleteRows(numRows   1, maxRows - numRows);

CodePudding user response:

@Antoine, just after range.clearContent(); add

console.log(numRows   " | "   newRangeVals[0].length)

I preseume that there is no data corresponding the the name of sheet. If true, add a test beforewards

CodePudding user response:

newRangeVals will only be populated if there's a row in which:

  • Value in column C matches the spreadsheet name (srchPatt_1.exec(r[srchCol_1])).
  • Column A is populated (r[0]).

If you are getting this error, that means newRangeVals is empty, which most likely means there's no row where both these conditions are matched. Double-check the sheet data so that the conditions are matched for some rows.

  • Related