1

Phrased another way, is there a way to:

  1. Create a new Google Spreadsheet (from the Google Drive API)
  2. And then create a Google Apps Script associated with that Google Spreadsheet programmatically
  3. And then programmatically activate the "On Edit" and "On Form Submit Triggers" to call various methods in the script? (this is the part I cannot figure out)

-OR- (as @JacobFlatter suggested)

  1. Manually create a spreadsheet with the desired script
  2. Use the Google Drive API to programmatically copy the spreadsheet (which copies with script with it as well)
  3. Somehow programmatically activate the "On Edit" and "On Form Submit Triggers" (which DO NOT copy over from copying the spreadsheet, this is the part I cannot figure out)
Jonathan Leung
  • 2,001
  • 2
  • 19
  • 24

2 Answers2

1

Possible path to follow:

Create a container bound script with an onOpen() trigger within a spreadsheet.

Copy the existing Spreadsheet (which will copy the script as well) programmatically.

Open the new Spreadsheet programmatically (unsure if this will initiate the trigger described above).

This assumes a few things, but seems like it is worth investigating. I'm curious if this works. Good Luck.

Jacob Flatter
  • 171
  • 1
  • 11
  • Hi Jacob, thanks for the suggestion! So I tried copying an existing spreadsheet and the script does indeed copy programmatically. However, the triggers do not ("On Edit" & "On Form Submit"). Any ideas how to activate these programmatically somehow on the new spreadsheet? – Jonathan Leung Jun 17 '13 at 22:02
  • 1
    Nice, I think at this point you should be able to use the ScriptApp api. Check out [https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder](Build Triggers With ScriptApp Api) . I hope that helps. – Jacob Flatter Jun 18 '13 at 15:45
  • OHH! Are you saying that inside of the onOpen() trigger (that already exists in the script) to then run the triggerBuilder stuff? Is that what you mean? That's genius, will go try it! – Jonathan Leung Jun 18 '13 at 18:04
  • Or you can build a new onOpen trigger after you copy the file. I am glad you have a few avenues to try. – Jacob Flatter Jun 19 '13 at 15:36
  • How do you build an onOpen trigger with the REST API? I don't think I understand how to make a REST command to the Google Scripts API, can't only the Google Drive API do that? Thanks! – Jonathan Leung Jun 19 '13 at 19:48
  • Ok, I am a little confused what you are trying to accomplish. Are you originally creating a new Spreadsheet from REST or from Google Apps Script? – Jacob Flatter Jun 20 '13 at 14:00
  • Either way, you can use Apps Script to create a trigger, after copying the file. Start by creating a google apps script (separate from spreadsheet script.google.com) and either call the function from Google Apps Script, or use the doGet() function and publish the script as an HTTP endpoint. – Jacob Flatter Jun 20 '13 at 14:02
  • I can outline this later if this is starting to make sense. – Jacob Flatter Jun 20 '13 at 14:03
-1

Unfortunately, no. You cannot do meta programming with Apps Script. Based on the answer to this question, I wrote a simple function to try meta programming

function meta(){
  var mime  = 'application/vnd.google-apps.script'; 
  DocsList.createFile('MetaScript','function test(){}', mime);

}

It throws up an error saying Invalid mime type

Community
  • 1
  • 1
Srik
  • 7,635
  • 2
  • 18
  • 29
  • Sorry, I don't think I phrased my initial question clearly... I mean using the REST API, can I create a Google Spreadsheet, and then use the REST API to add a Google Apps Script to that Spreadsheet? I'm not quite sure how meta programming is relevant to this but maybe I'm missing something? – Jonathan Leung Jun 16 '13 at 22:08