Home > Software design >  Is it possible to use Google Apps Script to add different editors to copies of a master Google Sprea
Is it possible to use Google Apps Script to add different editors to copies of a master Google Sprea

Time:07-27

I am working with Google Apps Script.

I have a master spreadsheet, of which I want to: make a number of copies, rename the copies, save them to a specified folder, and finally add a different editor to each of the copies

The list of names and editors comes from a table that contains in one column the name of the spreadsheet, and in the other the email addresses of the desired editors.

I have so far managed to write the following code:

function testCreaSpreadsheet() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var names = sheet.getRange("A2:A11").getValues();
    var emails = sheet.getRange("B2:B11").getValues();
    var ssToCopy = DriveApp.getFileById("XXXXX");
    var folder = DriveApp.getFolderById("YYYYY");
    
    for (var i in names){    
      var fileCopiato = ssToCopy.makeCopy("Foglio Offerte - "   names[i],folder)       
      }

}

This accomplishes the first three goals that I wanted to obtain, but I am struggling to find a way to add the editors directly from the data that I have in the table. Is this possible at all and, if so, what would be the best way to do it?

CodePudding user response:

In your script, how about the following modification?

Modified script:

function testCreaSpreadsheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var values = sheet.getRange("A2:B11").getValues();
  var ssToCopy = DriveApp.getFileById("XXXXX");
  var folder = DriveApp.getFolderById("YYYYY");
  values.forEach(([name, email]) =>
    ssToCopy.makeCopy("Foglio Offerte - "   name, folder).addEditor(email)
  );
}
  • In your script, I thought that names and emails can be retrieved by one call. And, by retrieved values, the file is copied and the editor permission is also added.

  • In this modification, the values of name and email are retrieved by getValues, and using the retrieved values, a file is copied and each copied file has the editor permission of each user of the same row.

References:

  • Related