0

I'm trying to make copies of a template file and share them with all our employees.

I've created a Google Sheet which lists the names off the employees, their email address, the email address of their manager and the company he/she is working for.

I've written the following script:

function CopyAndShare (Template, Name, Email, ManagerEmail, Company) {
  var CompanyFolder = DriveApp.searchFolders("title = '" + Company + "'").next();
  Template.makeCopy("Vacationdays 2015 " + Name, CompanyFolder)
         // .addEditor(Email)
         // .addViewer(ManagerEmail);
  Logger.log("Copy created for: %s.", Name);
}

function DistributeFiles() {
  var BaseFile = DriveApp.getFileById("1KBCFA21HbGZRCQXAtghatd8BfpR1PN2N_3L7rYxj1PU"); 
  var Employees = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
  for (var i = 1, l = Employees.length; i < l; i++) { // i = 1 to skip header
    CopyAndShare(BaseFile, Employees[i][0], Employees[i][1], Employees[i][2], Employees[i][3]);
  }
}

As a test I ran this script with the addEditor and addViewer functioncalls disabled. After 120 or so copies (and 6 minutes running) the script halted saying the Runtime quota exceeded.

I am a bit surprised creating 120 copies took 6 minutes. I want to make about 250 copies so I was halfway there.

What can I do to speedup my script so I won't run in the 6 minute limit.

I'm doing this on a Google Apps for Work domain.

  • Spps script has a feature to see time spent per function. Use it and post here results. – Zig Mandel Jan 26 '15 at 13:04
  • Seems the actual copying of the file is the culprit File.makeCopy([Vacation 2015 First Lastname, Company Name]) [7,037 seconden] I haven't seen anything below 2 seconds for the makeCopy call. – Dennis Janssen Jan 26 '15 at 13:51
  • In that case there isnt much you can do about speeding it. I thought maybe the search was taking time but its the copy. – Zig Mandel Jan 26 '15 at 13:53
  • (Both questions have basically the same answer and they are accepted) – Rubén Nov 23 '17 at 19:28

1 Answers1

1

Bruce Mcpherson has written a tool for parallel processing in Google Apps Script. It was designed to get you around the 6 minute limitation.

http://ramblings.mcpher.com/Home/excelquirks/htmlservice/parallel http://ramblings.mcpher.com/Home/excelquirks/htmlservice/parallel/implement

Spencer Easton
  • 5,642
  • 1
  • 16
  • 25