Home > Enterprise >  TypeError: Range is not a function In Google Apps Script
TypeError: Range is not a function In Google Apps Script

Time:04-20

I am trying to write a script that will a) extract the handle and field from the change_log sheet, and then B) use these variables to find the row and which the handle exists, and the column in which the field exists (on the same row), in a different sheet ('Master spreadsheet'), then C) Output the row number and column number's determined in part B.

It appears I have already achieved objective A, but I am running into this error during part B (error occurs on line 12):

Error   
TypeError: Range is not a function
changeUpdater   @ Code.gs:12

This is the function I have written so far:

function changeUpdater() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('change_log'); 
  let handle = sheet.getRange("E4");   
  handle = handle.toString();                                                 
  var field = sheet.getRange("I4");                                                         
  field = field.toString().substring(6, 41);                                                      
  sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Master spreadsheet');
  let column = 4;
  let row = 1;
  let columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues();   
  let rowValues = sheet.getRange(1, row, sheet.getLastColumn()).getValues();                                                  
  let index = columnValues.findIndex(handle);                                                
  let specColumn = rowValues.findIndex(field);

  console.log(index);
  console.log(specColumn); 
}

Any help is appreciated, I know this is probably a novice-level issue :)

CodePudding user response:

According to JS docs:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

So you need to pass a testing function name or a lambda to your findIndex call. And you're passing a Range instance.

let index = columnValues.findIndex(index => index === handle);  
  • Related