Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an existing app script to google sheets

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.

like image 934
sirnewbee Avatar asked Sep 18 '26 20:09

sirnewbee


1 Answers

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:
  • newTrigger(functionName)
  • ClockTriggerBuilder
like image 124
Emel Avatar answered Sep 21 '26 15:09

Emel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!