Is there a method or way of identifying the last edited sheet in a Google Spreadsheet using Google Apps Script?
You would have checked the official documentation and found that no such method exists.
To accomplish this, you would need to write a proactive script to:
Use an onEdit() trigger function to identify the occurrence and source of edits.
Record the details in a non-volatile store; perhaps another spreadsheet.
Respond to queries about the last edited sheet.
Related
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.
Dear Google AppScript Experts,
Recentlly I'm wondering whether there is a way to know exactly which sheet of a google spreadsheet changed or not. For example, thera are 30 sheets, such as 'Sheet1', 'Sheet2', 'Sheet3' and etc.
I can use Google Drive AppScript to retrieve the last update timestamp of spreadsheet to know whether it's changed or not, which it's on the whole spreadsheet, I cannot get to know which sheet change.
Anyone who has a solution on this problem?
Many thanks in advance.
It's well documented: GAS-triggers
Use the OnChange trigger and the 'e' object will have the 'source' with the spreadsheet name. Even though the doc doesn't mention it, you can use source with OnChange as well as OnEdit.
I am writing a Google Spreadsheet add-on that will pull data from web pages (calling URLFetch with an hourly trigger) and write the data to a spreadsheet.
However, I only want this add-on to only write to the same spreadsheet even if the user is opening the add-on in a different sheet.
Is it possible to force the user to work with that add-on only in a particular sheet? TIA.
Yes it is possible, when calling the SpreadsheetApp service you can call the sheet you want by name or the one you need from the list of sheets.
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
SpreadsheetApp.getActiveSpreadsheet().getSheetName()
Then you would write the information in that sheet.
hope it helps.
I wrote a small script bound to a Google spreadsheet that reads an email-address from a cell in the last row and sends an email to it.
The values are collected by a WebApp (not by Google form).
I tried all kind of triggers. The time driven triggers work, but the spreadsheet triggers don't work. I tried all of them. If I change manually some cell in the spreadsheet the onEdit trigger is working, but it doesn't fire up when the sheet was changed by another script.
Google's dev says : The onEdit() trigger runs automatically when a user changes the value of any cell in a spreadsheet. but it doesn't precise the change could be from script or not.
You'll find here a comment from HDCerberus :
I'm not at my PC to write a full response, but at a glance it could be the OnEdit trigger, which ONLY works when the sheet is edited manually, and NOT by a script. Are you expecting it to run when the sheet is edited manually or by a script?
Not every change triggers onEdit(). Please take a look on answer from topic Detect user inserting row or column in a google spreadsheet and reacting in a script - you will find registered bugs and one of them says that values written by scripts do not trigger onEdit().
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