Create installable triggers without opening the Spreadsheet

532 views
Skip to first unread message

sben...@bbchs.org

unread,
Aug 5, 2021, 11:33:27 AM8/5/21
to Google Apps Script Community
I have code creating a copy of a Google Spreadsheet.  That Spreadsheet has an installable onEdit trigger.(I need it send emails)  
When I make a copy is there a way to create that installable trigger without manually opening the Spreadsheet?  
I know I could create the onEdit Trigger using an onOpen event.  But then the trigger would belong to a different person.  

Would using the API be a better approach?
I have not tried that and I am not sure how to go about it.  

Alan Wells

unread,
Aug 5, 2021, 12:12:21 PM8/5/21
to Google Apps Script Community
Whose account does the code run from that creates the spreadsheet?
Whose account do you want the trigger to run from?
Whose account will the new spreadsheet be in?

Bennett, Scott

unread,
Aug 5, 2021, 12:16:18 PM8/5/21
to google-apps-sc...@googlegroups.com
They would all run from my account.  
I would own the sheet and run all the triggers.  
I

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/Hq5dObUOrUQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/03820507-2c90-4519-af1c-95b8e4ab3ed5n%40googlegroups.com.


--
Scott Bennett
Data and Assessment Coordinator/Math Teacher
Bradley-Bourbonnais Community High School

Alan Wells

unread,
Aug 5, 2021, 12:49:38 PM8/5/21
to Google Apps Script Community
The code that creates the copy of the Sheet can get the Sheet and use that to "install" the trigger at the time when you create the copy.  The Sheet doesn't need to be open as long as you can get the Sheet.  Whatever you are using to create the new copy should return a class that is the Sheet.

Bennett, Scott

unread,
Aug 5, 2021, 1:14:55 PM8/5/21
to google-apps-sc...@googlegroups.com
Just to clarify, I am creating a new copy of the Spreadsheet
Here is the code that I am using.  
Could you give me just the syntax needed?

function createSheet(empName,empEmail,dept,dates,halfday,remarksreasondirectName,directEmail,finalName,finalEmail) {
  
  let sheetFormTemplate = SpreadsheetApp.openById(template)
  let empForm = sheetFormTemplate.copy('Time off Request '+empName);
  
  let form = empForm.getSheets()[0];
  form.getRange('D8').setValue(empName);
  form.getRange('D10').setValue(dept);
  form.getRange('D12').setValue(dates);
  form.getRange('D14').setValue(halfday);
  form.getRange('D16').setValue(remarks);
  form.getRange('D19').setValue(reason);
  form.getRange('D23').setValue(directName);
  form.getRange('D29').setValue(finalName);
  form.getRange('A24').setValue(directEmail);
  form.getRange('A30').setValue(finalEmail);
  form.getRange('A36').setValue(email);
  form.getRange('A9').setValue(empEmail);
  DriveApp.getFileById(empForm.getId()).setSharing(DriveApp.Access.DOMAIN_WITH_LINK,DriveApp.Permission.EDIT)
  return empForm



You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/2160d9c6-7c79-4e76-9650-1bf71c716cf6n%40googlegroups.com.



Bennett, Scott

unread,
Aug 5, 2021, 1:38:57 PM8/5/21
to google-apps-sc...@googlegroups.com
Alan, I think I figured it out.  Thank you for your help.  

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/2160d9c6-7c79-4e76-9650-1bf71c716cf6n%40googlegroups.com.


--

Alan Wells

unread,
Aug 5, 2021, 1:41:04 PM8/5/21
to Google Apps Script Community
function createSheet(empName,empEmail,dept,dates,halfday,remarks, reason, directName,directEmail,finalName,finalEmail) {
   var createSpreadsheetOpenTrigger;
     createSpreadsheetOpenTrigger = function() {
           /**
     * Creates a trigger for when a spreadsheet opens.
     */
       ScriptApp.newTrigger('Put_The_Name_Of_Your_Function_here')
        .forSpreadsheet( empForm  )
        .onOpen()
        .create();
    }

    let sheetFormTemplate = SpreadsheetApp.openById(template)
    let empForm = sheetFormTemplate.copy('Time off Request '+empName);

    createSpreadsheetOpenTrigger();//Call fnk to create trigger
Reply all
Reply to author
Forward
0 new messages