1

I have a Spreadsheet file in Drive that, via script, creates other file, in a subfolder, by copying a template file, and renaming it. This new file also uses some scripts that require OAuth. Problem is that the new file executes a funcion onOpen(), but it won't run properly because it needs the OAuth approval. The template files are authorized already, but I had to create a button to trigger one or more times the onOpen function. Is there a way to "skip" the OAuth so everytime I clone the template, the new file has OAuth permissions already?

This is the code on the Master file thst creates sub files:

var newfile = template.makeCopy("newname",folder);
newfile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

This is the code on the new file:

function onOpen(e){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getSheetByName('sheet1').getRange('A1');
  var name = ss.getName();
  
  if(range.getValue() != name){
    range.setValue(name);
  }
}

The issue is that, when I open the file, the contents of A1 don't change. I have to open the script, run the onOpen function, authoriza OAuth and re-run function.

Any way to automate this, so I don't have to re-authorize every new file?

Thank you in advance.

EDIT: In my Drive I have a file named "ProjectManager", and a folder named "Projects" where I have a file named "Template". ProjectManager has a button to duplicate Template and rename it. It runs the following script:

var template = DriveApp.getFilesByName('Template').next();
var newfile = template.makeCopy("name", folder);
newfile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

"name" and "folder" were previously defined.

Now, Template (and therefore, the newfile) runs the following script:

function onOpen() {
  var ss    = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getSheetByName('sheet1').getRange('A1');
  var name  = ss.getName();
  
  if (range.getValue() != name) {
    range.setValue(name);
  }
}

Both ProjectManager and Template (and the copies) run other functions. But when I open the newfile, the onOpen function doesn't have effect on the file. I added a button to trigger the onOpen function again after the file opened, but the OAuth window pops up; this doesn't happen with the Template, that is already authorized.

Since I'm the only one that will use this files, I'd like to automatically authorize the use of scripts, so I don't have to authorize every file I create.

By the way, I am kind of a noob here; just FYI.

0 Answers0