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.
Related
I have a google analytics report that runs in a sheet. Using the Chrome plugin, this is set to run automatically. The results are then pulled into Data Studio on "data refresh". This works. However, I want a google script to run in the sheet.
I have written a google script routine to manipulate the Analytics data that is imported to the sheet. It runs onOpen. However, I need some automatic trigger more like "onReportRun" to trigger on the scheduled report for the Analytics -> Sheet process.
function onOpen(e) {
var paramRange= SpreadsheetApp.getActiveSheet().getRange('A2:B');
var paramValues= paramRange.getValues();
//crunch Analytics data and make two new columns and put them in the sheet. This works fine.
}
//need this:
function onGoogleSheetsAnalyticsAddOnScheduleReportRun(e) {
This runs when I open the sheet. I need it to run when the Google Sheets plugin for Analytics runs the configured report.
If you are interested as to how I ended up here, an Analytics Custom Dimension contains a list. Example: a web page has three qualities tagged against it: "health, wealth, safety" in Dimension 3. Data Studio and Analytics can't seem to split and then report on list items individually. So I export from Analytics to Sheets, run code to do this, then import the sheet to Data Studio.
How can I run the googlescript code in a sheet without opening the sheet
By simply changing onOpen to other triggers.
Google Sheets and Google Apps Script haven't triggers that runs when other code run. Perhaps your best option is to use time-driven trigger to call a "poll" function. The poll function could check certain value on your spreadsheet your could access an API to check if it should update the spreadsheet. The actual function will depend on your spreadsheet and/or the external service that you want to monitor.
I have a font and alignment script which works like a charm on our daily spreadsheet. However I copy the blank file (template) 30 ish times for each month. As soon as I have done this, the font script stops working unless I go in and authorise it for each new sheet. Is there any way, maybe with the script library i can run it automatically and not have to authorise each new sheet? All users have edit access to the sheet. I have been reading and playing with the script library but can't seem to get it to work at all.
Thank you for any help.
The simplest way to authorize a script and then not need to reauthorize a derivative of it, is to not create a derivative of it. Currently, you indicate the workflow is create a copy of yesterday's document, and then everyone works on the copy.
If you reverse the order, such that you archive the copy, then you will no longer need to continually reauthorize your script in a new file, as the working file contains an authorized Apps Script project.
Create a template file, which has the corresponding sheets you would like to start each day with
Write a script which will perform 2 tasks
Export the current state of the active workbook to a new workbook (or even a PDF or other archive-ready asset)
Replace the contents of the active workbook with those from the template workbook.
Run this script either manually or from a time-based trigger.
I'm coding a script for a spreadsheet. This script creates a menu.
Then by choosing an option in this menu a function (which uses API) will run in order to filter some columns and hide others.
The problem is:
This sheet is protected (because shared with coworkers) but I want to allow people to run the script, which is impossible without the permission.
I already looked at different solutions:
Using a trigger: Doesn't work because a trigger can't correctly call a function which uses API (yes, my functions use API).
Web App: When the script is run from the spreadsheet, the script is run as the current user, not the script editor. (the web app is efficient if the user uses the HTML page.)
Remove protection -> run the function -> Re-add protection: Can't modify the protection without permission, which is logical.
Add the current user in the list editor -> run the function -> Remove the current user from the list editor: Can't modify the editor list without permission, which is logical.
How can I solve this problem?
This is a pain that I have not seen a good workaround. Google should know that in a collaborative environment that the owner would create script and want users to be able to run those scripts while at the same time not messing with formulas or cells that you desire to protect. The only way I have found to solve this on the sheet itself is to Unprotect and then Protect the cell or range you are making modifications to during the script run. Do be mindful that if the script fails in the middle (after you have unprotected it) the cell remain unprotected. Might want to run within a "try" script.
Are you the spreadsheet owner?
If no, you are not authorized to bound an apps script on it.
But you can make a copy of the spreadsheet by duplicate it and save in your drive.
If the spreadsheet is created by you yourself, maybe you created it by another username. If it does, log in by that user and change the sharing option to allow the specific user can edit it.
How do I create a script that runs as soon as the user opens a spreadsheet?
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
This is simple but I've gone through the tutorials on their webpage and couldn't do it. I know how to program but I am new to Google Apps Scripts.
Also is this something done better in Google Forms or in Google Sheets?
How do I create a script that runs as soon as the user opens a spreadsheet?
An onOpen() trigger function.
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
The onOpen() should call a function that uses Browser.inputBox() to get the user's input, then writes it to the sheet via Range.setValues().
Note that the user will need to have edit privileges for this to work.
Also is this something done better in google forms or in google spreadsheets?
If you want the UI to show up in a spreadsheet, then the script must be contained in a spreadsheet.
Alternatively, if you don't want the user to see the spreadsheet, you could use the Forms service to collect their input, with no need for any programming.
Nothing too hard to get that :
read this doc on how to build an alert in a spreadsheet and make it show up using an installable trigger onOpen
all you have to do is put these together.
For script lauched at opening you need write function:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
...
};
Google Spreadsheet don't have a dialog window or something similar.
But there are several ways:
You can use show method to show your html or UiApp input dialogbox: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#show(Object)
you can use one sheet for input "sex" and hide others sheets. After click button you can show all sheets. Here is SpreadSheet API: https://developers.google.com/apps-script/reference/spreadsheet/
you can use information from profile logged person and read his/her sex :)
you can use Form for input "sex" and switch context (by script) to spreadsheet but this is more complex solution. Here is Form API: https://developers.google.com/apps-script/reference/forms/
Yea, I forget about inputBox() :)
To create a script that runs as soon as the user open the spreadsheet, you should create a bounded to a spreadsheet script and use a trigger:
Create a new or open an existing spreadsheet.
Click on Tools > Script editor...
Click the close button on the welcome dialog.
Start writing your script
To make the script run on open, you could use a simple trigger by using onOpen as the function name or you could use an installable trigger.
For further details see https://developers.google.com/apps-script/guides/sheets
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