Standalone Script for Google Sheets - google-apps-script

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.

Related

How can I set an app script on a Google Sheet via the API?

Is there a way to set an App Script on a new Google Sheet via the Google Sheets API? We use pygsheets as our Python API Client for Google Sheets. Since we are using the onSelectionChange trigger to populate certain cells with a date, we would like to setup the app script after we have populated rows in the spreadsheet.
The Google Sheets API doesn't have a method to do this. You can use the Apps Script API instead, but there are a few caveats which may or may not work with your use case:
You can use projects.create to create a new Apps Script project and attach it to a Sheets file by specifying the parentId field, which is the Sheet's ID. However, there are no methods to attach a preexisting script to a Sheet. You can still use projects.updateContent to add the required script files, though.
You cannot unbind the script once it has been attached.
This API does not work with service accounts, and the user running it needs to explicitly allow it in their settings page. This is for security reasons, since allowing a malicious app to freely write scripts on your account could be catastrophic, but it adds more steps that your users would need to take in order to run your app.

How to make Custom Apps Script Function trigger on edit?

So I created a Custom Google Apps Script project, and then deployed it as an add-on (for my domain only). However I soon realized that I need to change the trigger to be only onEdit. However that option isn't there for this Apps Script project. How do I get around this? In the picture there is only time-based trigger and calendar trigger. While for a script that's not deployed as an add-on there is an option to make it trigger onEdit
By using the "add trigger" button on the project triggers page we could only create on edit triggers for Bound to spreadsheet (Google Sheets) projects.
The alternative is to create the trigger by using code. For details please checkout https://developers.google.com/apps-script/guides/triggers/installable
it's worthy to note that the documentation in https://developers.google.com/apps-script/overview on the Types of Scripts lists three
Standalone
Bound to G Suited Documents
Web Apps
In this case the screenshot that is included in the question corresponds to a standalone project.

Inherited Apps Script onOpen function not working on cloned Google Sheet

I am using the Drive API, authenticated via a service account, to copy a "master" Google Sheet that has an Apps Script attached. The Apps Script is simple and creates a custom menu in the Google Sheet. However, when the Google Sheet is copied from Drive and then accessed via the Google Sheets API, the attached apps script doesn't execute the onOpen function. I'm guessing there is some strange ownership/permissions issue that is preventing the onOpen from "activating" when it's copied with the service account and then given domain-wide "write" permissions.
On open events, simple and installable, are triggered only when a user opens a spreadsheet not by API actions.
Reference
https://developers.google.com/apps-script/guides/triggers/

onOpen trigger for view-only users

The built-in onOpen trigger in Google Apps Script only runs when a user with permission to edit opens a spreadsheet or document. I am developing a spreadsheet where only developers have edit privileges and all other users must have view-only privileges.
How do I create an onOpen trigger that fires when view-only users open my spreadsheet as well as those with edit privileges?
You can't, because it's not supported. Google Apps Script, embedded in either Google Sheets, Docs, Forms, and as an add-on, runs only for editors.
The alternative is to Publish as a webapp, so the viewers open that instead of the sheet directly, or open the webapp (on another tab, not inside the sheet) from a link in a sheet cell.
From the "Restrictions" section of trigger documentation on triggers - both simple and installable:
They do not run if a file is opened in read-only (view or comment) mode.**

Activating Triggers in Google Spreadsheet Scripts with Google Drive SDK

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