Appscript Google addEdtor(row[5]) permission.value

2019-08-02 02:43发布

问题:

Sheet: list of user's names row[0] list of user emails row[5]

run the script it works fine apart from this line

 DriveApp.getFileById('1phsJs4ik2a654645645GOJs4gBVVKd2OmlHtlPk').makeCopy(sheetName,dest).addEditor(owner);

ERROR: Invalid argument: permission.value (line 21, file "Code")

Works fine when i replace the 'owner' with '"email@google.com"' but i need it to iterate through a list

Full code:

// Showing it has been sent.
var Confirmed = "Confirmed";

function DistributeTemplate() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var specify = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var counter = specify.getRange("A1").getValue(); //HOW MANY TO DO 
var numRows = counter;   // Number of rows to process
 var dataRange=sheet.getRange(startRow, 1, numRows, 5)
 var data = dataRange.getValues();
 var dest = DriveApp.getFolderById('INSERTFOLDERID');
 for (var i = 0; i < data.length; ++i) {
 var row = data[i];
 var sheetName = row[0]   
 var FlexisheetSent = row[2];   
 var owner = row[5];
 Logger.log(owner);
  if (FlexisheetSent != Confirmed) { //Only send once
    //DriveApp.getFolderById(dest).addEditor(owner);
   DriveApp.getFileById('INSERTTEMPLATEIDGOOGLESHEET').makeCopy(sh eetName,dest).addEditor(owner);
   sheet.getRange(startRow + i, 3).setValue(Confirmed);
     // The cell is updated instantly to avoid any issues.
    SpreadsheetApp.flush();
  }
 }
 }

回答1:

There is no value row[5].

At this line:

var dataRange=sheet.getRange(startRow, 1, numRows, 5)

The configuration of the parameters of the getRange() you're using is: getRange(row, column, numRows, numColumns)

That gets you an Array of 5 elements [[0, 1, 2, 3, 4]], but you refer to an Array element by referring to the index number and Array indexes start with 0.

You either need change your var owner = row[5]; to var owner = row[4]; if the email is in your E column or var dataRange=sheet.getRange(startRow, 1, numRows, 5) to var dataRange=sheet.getRange(startRow, 1, numRows, 6) if the email is in the F column of your Spreadsheet.

Also be sure that the cell that has the email doesn't have leading or trailing whitespaces, or any other special character.