API works when running script, but not from associated spreadsheet - google-apps-script

I am trying to apply conditional formatting rules on a google sheet using a google apps script. I found that this could be done with the API, so I enabled it and have it all working when ran from my script ... but when I trigger the script to run from my sheet, the API batchUpdate doesn't run. I am sure I'm missing something simple ...
The code is in the Themes.gs project of this spreadsheet: https://docs.google.com/spreadsheets/d/1Fs6B-OmlqfoAoon5PcoAYbHq4CO8w0qPqfSRy_OQhe0/copy
specifically this bit doesn't seem to run when the script is triggered from the sheet itself:
Sheets.Spreadsheets.batchUpdate(JSON.stringify(functionname), SpreadsheetApp.getActiveSpreadsheet().getId())

A simple solution is Installable Triggers.
Rename your onEdit() function to something else, like whenWeEdit().
Click the link that says: No triggers set up click here to add one now.
Under Run, select whenWeEdit (or whatever you renamed your function as).
Under Events, select From spreadsheet.
Next, instead of On open, select On edit.
Click save.
Edit the sheet. Your function now seems to work for me. What about you?

Related

Google Apps Script: How are edit events from a bounded script in one sheet treated on another?

I'll try to be as specific as possible. This questions relates to Google Apps Script, bounded to two sheets.
I have a function that runs on edit through an installable (not simple) trigger. (Installable because it needs to call e.g. GmailApp). Call it Sheet A.
On another sheet, which we'll call Sheet B, is a script which causes an edit to be made in a cell on Sheet A.
When an edit is made manually on Sheet A, the installable on-edit script runs fine.
When the edit is made via the script on Sheet B, the installable on-edit script does not run. In other words, there doesn't seem to be any event associated with edits made by a script.
I am wondering if there is any way to trigger the on-edit script on Sheet A to run when changes are being made through another, external, bounded script.
I've looked through GAS documentation and searched as best I can here, but I cannot find an answer. I've tried to use a simple trigger, but it also does not seem to "pick up" the edit event when performed by another script.
Any help would be greatly appreciated.
Triggers only run on "human" changes. You need to rethink your flow.
If you have a function funA() changing something programmatically in a cell and you need to trigger another function funB(), just call funB() directly from funA() without counting on a trigger.
onEdit is triggered only if user updates a cell, not when the cell is updated programmatically.
You should expose onEdit from sheet A (or extract the method that actually does send the emails) and publish it as API executable
Then you can call that method from sheet B, after the edit is made programmatically.

Why is Google Sheets' onEdit function not getting executed?

I have a Google Sheets spreadsheet that's been working for months, and has no scripts associated with it. Recently I decided I'd like to log the date of change to a row, and that the onEdit() function could do that for me.
From the spreadsheet display, I selected Tools / Script Editor, which popped up an empty script with a myFunction() function.
I modified myFunction() to:
function onEdit(e) {
Logger.log("Something was edited.");
}
Then pressed Save and accepted the default progject name (Untitled project). I also tested this (Run) and it created a log entry.
Now when I edit any value in the sheet or add a new row, I would expect a log entry. But none appears.
Have I missed a step? What must one do in order to get a valid onEdit() to run?
Your logs will be available at View>Stackdriver logging. View>Logs will only show logs executed in the current session (by clicking "Run"). Using console class is preferred.
Disclaimer: I don't often work with the Script Editor in Google Sheets, nor Google Sheets itself, too often. There may be a better way to accomplish this.
In my experience, I've found you also have to explicitly add a trigger to your Script Editor project to get the events to fire correctly.
From your sheet, open your Apps Script project by selecting the Tools > Script Editor menu option.
From the resulting Apps Script project screen, select the Edit > Current Project's Triggers menu option.
From the resulting Triggers view, click the + Add Trigger button (appeared in the lower-right of the interface presented to me; your mileage may vary).
In the resulting modal, select the following options:
"Choose which function to run" - select the onEdit method to execute the method you've written.
"Choose which deployment should run" - I've selected the "Head" option, but I'd imagine your mileage will vary wildly if you've elected to use the more advanced deployment features here.
"Select event source" - set this to "From spreadsheet".
"Select event type" - set this to "On edit".
"Failure notification settings" - set this to whatever setting you'd like.
Finally, click the "Save" button.
Once your trigger is set up, the spreadsheet should now be running your method whenever an edit to the spreadsheet is made.
Answer:
Make sure you run the onEdit(e) function from the Apps Script UI using the play button (►) and authorise the script to run as you.

Triger a function on a change in doc

I want to run a Google App Script function every time a change is made to my Google Doc. I have found onChange and onEdit triggers which are only available for Google Spreadsheet. I am sure there must be an edit/change trigger for Google Doc as well.
Unfortunately there are no onEdit(), onChange() triggers for Google Docs.
The triggers available for Google Docs are the following:
onOpen() triggers both simple and installable;
time driven triggers;
onInstall() simple triggers.
What you can do instead is to use a time driven trigger so in this way even though the changes are not directly triggering the execution, the trigger will still run how often you want it to.
Last but not least, you can file a Feature Request on Issue Tracker where you specify the details needed.
Reference
Apps Script Trigger;
Google Issue Tracker.

Some methods/functions simply do not work in onEdit trigger

I'm looking to call UrlFetchApp.fetch() based on user input on a sheet but nothing happens. No errors, just silently ignores the call. Same goes for MailApp.sendEmail() and GmailApp.sendEmail()
This is on a Google Apps domain, and only domain users are using the Google Sheet.
Simple triggers like onOpen or onEdit are not able to do anything that requires authorization like sending mails because they run silently and anonymously.
This is all explained in the documentation .
You should simply rename your onEdit function to something else - SpecialOnEdit for example ? - and create an installable trigger (documentation) from the script editor menu (ressources/triggers/create a new trigger...)
You can see an execution flow in view -> execution transcript in the script editor. Just change a cell value in spreadsheet then come back the script editor and check "execution transcript". It will show you an error if it happens.

How to create a Google Sheets Script that runs as soon as the user opens a spreadsheet?

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