Phrased another way, is there a way to:
Create a new Google Spreadsheet (from the Google Drive API)
And then create a Google Apps Script associated with that Google Spreadsheet programmatically
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)
Manually create a spreadsheet with the desired script
Use the Google Drive API to programmatically copy the spreadsheet (which copies with script with it as well)
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)
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.
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
Related
I am very new to Google Apps Script. I created a Macro in Excel with VBA. My process is downloading a CSV that I save to my hard drive, opening it (my PERSONAL workbook is open at all times) and then running my macro which will format the list/perform the steps of my macro. I would then save and manually upload that list back to Google Drive.
This is obviously inefficient and The rest of my team uses Google Drive/ Google Sheets and so I am attempting to teach myself how to build out the same macro in Google Apps Script. I understand the difference between bound and unbound scripts - this is similar to Excel in the sense that if I wanted to run my macro on multiple sheets, I would have to save it to my PERSONAL spreadsheet where I stored all of my macros and then have that sheet open at the same time that I ran the macro on a different spreadsheet.
However, for Google Apps Script, it appears as though you simply cannot run a macro through multiple different workbooks without manually copying and pasting your code to every new spreadsheet. Is this true? I see that it could be added in the app store and published as an add-on but my script is painfully basic, I'm just changing some words and formatting. It seems odd that this cannot be applied to multiple spreadsheets the way that it can in Excel. My goal was to use something like:
function onOpen(e) {
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('List Upload')
.addItem('Format List Upload', 'formatListUpload')
.addToUi();
where formatListUpload would run my formatting script so that my coworkers could just click "Format List Upload" as a menu option and the new spreadsheet they had just opened would be quickly formatted. This seems like base-level functionality for macros/ app script. Is there truly no way that this can be done?
Many, many thanks!
From the question
However, for Google Apps Script, it appears as though you simply cannot run a macro through multiple different workbooks without manually copying and pasting your code to every new spreadsheet. Is this true?
No
Is there truly no way that this can be done?
Instead of using a simple trigger you might use an installable trigger, but this option is limited to create 20 triggers by script / user. To make this work you will need somehow to open the spreadsheet i.e. SpreadsheetApp.openById(spreadsheetId)
Options to avoid having to create an add-on
One option that doesn't requires installable triggers is to submit the spreadsheet having the macro to the templates gallery and use the template to create the new spreadsheets
Another option is use script to create a new spreadsheet and the installable trigger for it.
One more option is to create a new spreadsheet, grab the spreadsheet id, either manually or programmatically, then use it to create the installable trigger for the new spreadsheet.
Beginner question – I have an app script in a Google Sheet (response sheet from a Google Form) generating Google Docs. It is unexpectedly making me the owner of those docs. The business owner is the owner of the form, sheet, doc template, and shared Google Drive folder where the docs are. She has given me edit access to all. She also is shown as the owner of the app script “project” in the sheet. The app script creates a copy of the template doc in the shared folder, and populates it with data from the form. I am the owner of these new documents, why is that? I am only editor of all the components involved. Can I fix it so that she is the default owner of the new docs?
The newly created documents will be owned by the account that runs the script. This is clear enough when you run the script manually, but it also happens when you run it through an installable trigger. The trigger owner will own the files created while the function runs.
Check who owns the 'formSubmit' trigger.
(From your script editor, view your triggers. The left most column will show who the trigger is owned by.) Since your name is appearing as owner, the trigger is most likely still owned by you.
Iv'e written a custom google sheet function using Google Apps Script, and i would like to share it with another spreadsheet user, but i do not want to share the functions source code.
I've converted the Google Apps Script to a project, but now i have no idea how to link the project back to my sheet, so the function will work again.
I also do not want to publish the AddIn to the marketplace.
Test As Addin also doesn't work, the sheet is opened in a new tab, but the cells with the custom function says #NAME?.
What am i missing?
Explanation / issues:
The error #NAME? indicates that you are trying to use a function
that does not exist. As you also mentioned, this function does not
belong to the active spreadsheet but on a different project.
Unfortunately, it is not possible to share a custom function with other spreadsheets directly.
Possible Workarounds:
You can create an add-on.
Another workaround solution would be to create a library. That is a great alternative and the documentation is quite straightforward.
Related:
Creating add ons in Google Apps Script
How to call library function as custom function from spreadsheet formula?
I am looking to locally use a Google Apps Script with one of my Google Sheets (Attached to a form) that will be accessible to all emails that have permission to access the sheet, but I don't plan on publishing this script to the public. Do I still need to publish in order to use this script, or will I be fine just adding an onSubmit function for my Google Form as a trigger for my script to run in my Google Sheet? And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Do I still need to publish in order to use this script
No: If you own the spreadsheet (or have editor permissions on it), your script can remain private. A script does not need to be bound to a Spreadsheet except if it will use:
Spreadsheet UI (getUi(), custom menus, dialogs and sidebars)
"Active" items (getActiveSpreadsheet(), getActiveSheet(), getActiveRange(), getActiveCell())
...will I be fine just adding an onSubmit function for my Google form as a trigger for my script to run in my Google Sheet?
Yes: Your standalone script can have installable triggers associated with Google Apps items such as spreadsheets, documents and forms.
And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Yes: There's nothing special about the 1:1 relationship; the same trigger could be installed for multiple spreadsheets.
I know it's a long shot however, do you know if it is possible to attach a script to a spreadsheet you create in a script?
pseudo code version would be
function create_spreadsheet
new spreadsheet = ... creates spreadsheet
new spreadsheet = ... fills with appropriate formatting
-Need help part is -
new spreadsheet add script = ... Attach Following script to the new spreadsheet.
-Need help part is -
You can use the AppsScript REST API (aka "advanced API") to programmatically manage AppsScript projects. See https://developers.google.com/apps-script/api/how-tos/manage-projects This includes the ability to create "bound" projects (i.e., bound to a specific Sheet) and upload new content to that project's files.
However, to use the REST API from AppsScript code you'll need to enable the "advanced API". See https://developers.google.com/apps-script/guides/services/advanced.
I have not gotten to try it yet, but rather than creating a spreadsheet you could copy a public version of a spreadsheet that includes the script.