4

Is it possible to add an existing app script to a newly created googlesheet using app script? And automatically assigning it to a trigger?

For example I have a spreadsheet call spreedsheetA. Then my form will create spreedsheetB, is it possible to automatically add my app script to spreedsheetA to spreedsheetB with out copy + pasting it manually. All by using the appscript in my form.

3
  • 1
    For example, in your situation, how about copying "spreedsheetA" as "spreedsheetB"? About Is it possible to add an existing app script to a newly created googlesheet using app script?, you can achieve this using Google Apps Script API. Ref About And automatically assigning it to a trigger?, for example, how about creating both the Spreadsheet and the container-bound script using a script? Commented Feb 3, 2022 at 11:47
  • I didn't think of that. Let me test that out. That's brilliant! Thank you kind sir! Commented Feb 3, 2022 at 11:52
  • Thank you for replying. I think that if copying "spreedsheetA" as "spreedsheetB" is the same goal you expect, that is the simpler solution than using Google Apps Script API. Commented Feb 3, 2022 at 11:56

1 Answer 1

1

As a workaround to what @Tanaike proposed you can use the Drive API to make the copy. Because bounded scripts are also copied when you perform a makeCopy() operation.

Steps:

  1. Create a new sheet and add a bounded script via Extensions > Apss Script. Take note of the ID, in the example I will call it SSA_ID
  2. Add your script. As probe of concept I just added this simple one:
function onOpen(e) {
  SpreadsheetApp
  .getUi().alert('Hi from bounded script')
}
  1. Create a new script, and paste this code inside:
function copySpreadSheet() {
  const file = DriveApp.getFileById(SSA_ID)
  const newFile = file.makeCopy(`SpreadSheetCopy_${new Date().toISOString()}`)
  Logger.log(newFile.getUrl())  
}
  1. Run the script, grab the url and copy paste it in your browser. You will see that it contains a copy of the bounded script.

From there you can manipulate the copy and add it to an Installable Trigger, for example:

ScriptApp.newTrigger('copySpreadSheet')
  .timeBased()
  .everyHours(6)
  .create();

Documentation:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.