1) Loop through array of sheet/file IDs 2) Use DriveApp to getLastUpdated for each file 3) setValues in adjacent column

51 Views Asked by At

The log spits out the information that I want (a timestamp of when the file was last updated), but when trying to setValues() in the adjacent column, I get "Exception: The parameters ((class)) don't match the method signature for SpreadsheetApp.Range.setValues."

More specifically...

var sheetID is a list of spreadsheet IDs from various sources

var pasteRange is the array in which I want to paste in the date/time that the sheet was last updated.

function getLastModified() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var getTab = ss.getSheetByName("Tab1");
  var sheetID = getTab.getRange("L2:L101").getValues();
  var pasteRange = getTab.getRange("K2:K101");

  for (i=0;i<sheetID.length;i++){
     if(sheetID[i] == ""){

     } else {
      var file = DriveApp.getFileById(sheetID[i]);
      var date = file.getLastUpdated();

//      
pasteRange.setValues(date);
      
//the log works
Logger.log(date)
     }
     }

  }

1

There are 1 best solutions below

6
dataful.tech On

setValues() requires a 2D array and you are passing one value. You can try this code that loops through all rows, accumulates values in dates array and then records all of them at once. As a benefit, it will be much faster.

function getLastModified() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var getTab = ss.getSheetByName("Tab1");
  var sheetID = getTab.getRange("L2:L101").getValues();
  var pasteRange = getTab.getRange("K2:K101");
  const dates = [];

  for (i = 0; i < sheetID.length; i++) {
    if (!sheetID[i]) {
      dates.push([null]);
      continue;
    }
    var file = DriveApp.getFileById(sheetID[i]);
    const date = file.getLastUpdated();
    dates.push([date]);

    //the log works
    Logger.log(date);
  }
  pasteRange.setValues(dates);
}