I have a code into a spreadsheet (A) that does a copy of a sheet from another spreadsheet (B) the is activate from a trigger onOpen.
But trigger onOpen starts after 5 or 6 seconds, so a user can do some changes into the sheet.
I would like to keep my sheet for those 10 seconds too.
Have you an idea? I'm looking how to intercept calls before trigger open but I don't find documentation.
Thanks
You can install a onEditSs edit trigger on B (dont use the simple onEdit, instead install one).
From there copy to A whenever the range changes (keep the last one copied on cache to minimize writes).
This way A is always up to date even without opening it.
Related
I have a trigger on a Google Sheet set up to run a function weekly. I am hoping to scale this trigger out to my team without having to go into each teammate's individual sheet's script editor and enable it. Do you know if triggers can stick when making a copy of a master sheet that has a trigger set up? Can triggers be saved and used via shared libraries? I haven't had much success and would love some pointers if you have them.
Thanks!
Unfortunately, the answer is no. Only the owner of the google account can create triggers for himself/herself.
However, there is a great alternative. You can add this code to the master file (script editor):
function createTrigger() {
ScriptApp.newTrigger("Name_Of_The_Function_You_Want_To_Trigger")
.timeBased().onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(7).create();
}
and when the person of your team executes this function (in his own copy of the master file), the script will create for him a particular trigger for:
Function: "Name_Of_The_Function_You_Want_To_Trigger"
type: timeBased()
Hour: 7am
When: weekly, every Monday
That saves you a lot of time, since the user (your colleague) has to run this particular function just once from his own file.
We're working with Spreadsheets and Apps-Script to do some automation, therefore we've set up some time based triggers, some hourly, some daily based, all set up in the apps-script dashboard. It happens more and more often, that the triggers simply don't run. They're just not executed, so I don't see any failure in the dashboard (because the code is valid, though).
I thought, it was about the .getActiveSpreadSheet() method which might not work, when the sheet is not open or visible (which it would be in the most cases), but I recently have a sheet where I open it by id (openById), and the trigger still works just as he pleases.
Simple example code is:
function testTrigger(){
var sheet = SpreadsheetApp.openById($sheetid).getSheetByName("ha")
sheet.getRange("A1").setValue(new Date());
Logger.log("test");
}
And before I used
function testTrigger(){
var sheet = SpreadsheetApp.getActiveSpreadsheet.getSheetByName("ha")
sheet.getRange("A1").setValue(new Date());
Logger.log("test");
}
The trigger for this function is set to fire every minute. As I can see in the sheet, the last time it ran was 12:35:48, but it's now 12:40. So why isn't it working?
By the time I'm writing this, it suddenly fired at 12:40:48. But now it's 12:44 again, so there are already 3 runs missing! Why does this keep happening?
And: Would SpreadsheetApp.getActiveSpreadsheet.getSheetByName("ha") even work with a closed sheet? Has the sheet to be open AND active in the browser? We were using Spreadsheets and Apps-Script for months now, I used getActiveSpreadsheet.getSheetByName("ha") every time and it worked. It seems, the time driven triggers are buggy atm, I have no other conclusion (since the code and everything else is valid).
Is there any way to find out, why a trigger didn't fire (beside a code error, which I would of course see in the dashboard).
And would these problems also occur, if I would use programmatical triggers?
Thanks in advance!
Here's an image of the problem (nothing changed in the code or in the trigger during these runs):
To answer some your questions
As specified by the documentation for time-driven triggers "The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m"
If instead you create manually a ClockTriggerBuilder with the parameter nearMinute(minute), this "Specifies the minute at which the trigger runs (plus or minus 15 minutes)". In other words: You cannot expect from the Apps Script triggers to run precisely at the time you would like them to.
SpreadsheetApp.getActiveSpreadsheet() works if you have a bound script, rather than an alone-standing script. In any case, to avoid error sources, it is wise to use SpreadsheetApp.openById(id) instead of SpreadsheetApp.getActiveSpreadsheet()
You can supervise your triggers und executions and My triggers, however from your description it seems like the trigger is fired correctly, just not exactly at the time you would desire.
I have Google Spreadsheet named "TeamWork" and I share it with about 50 other users, they can edit different ranges ad sheets. Problem is that when some of them want to change the name of file, they just can do it, so time to time I have funny names for this file, like "teamNotWork" etc.
Is there a way to prevent anyone except owner from rename Spreadheet? If possible not using "onEdit" script trigger, because its using quota.
Thanks for help!
onOpen wont happen often enough and onEdit will consume too much quota.
instead, use a time based trigger (every minute or every 5 minutes) to rename the file if its not what you want.
it also has the advantage that it will work regardless of how users open the sheet (mobile or desktop).
I would try using onOpen trigger:
function onOpen() {
var file = SpreadsheetApp.getActiveSpreadsheet();
file.rename('TeamWork');
}
You may add one more time Driven trigger and launch it once a day when nobody works with file.
I've built a spreadsheet that tracks form responses using the onSubmit trigger. This works well. I then want to display part of the spreadsheet on another spreadsheet, and have built a script that does this.
In order to ensure the second spreadsheet is dynamically updated and synchronised with the first, I have tried to use the onEdit installable trigger; however, it does not work when it is programmatically edited, only if I manually edit spreadsheet 1. Any solutions?
Triggers do not fire on the spreadsheet changes made by scripts. I think the idea is that the script making changes can also follow through on any consequences of those changes. In your case, I see three solutions:
If you just want to "display part of the spreadsheet on another spreadsheet", then importrange command suffices, you don't need a script to do that.
The function triggered by form submission can modify the target spreadsheet itself. To do this, you need an installable trigger running on form submission, since simple triggers cannot access other spreadsheets.
If you really want to trigger a function in a script attached to another spreadsheet, you can run a time-based trigger that will check the last-updated time of the spreadsheet.
Example of #3: a function that can be set to run every 5 minutes, to detect changes of any kind.
function checkForUpdates() {
var updated = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getLastUpdated();
if (new Date() - updated < 300000) {
// updated in the last 5 minutes, do something
}
}
I have a script (Script by spreadsheet) who writes a series of data in a spreadsheet, but some parts of the spreadsheet are locked.
The users that are running this script from the spreadsheet are blocked in certain areas, it is possible that Script release these areas and then lock them again or run as adm of the Spreadsheet?
Thank you.
The user would need to have permission to edit sharing on the Spreadsheet, which would not be the case if it was locked.
Is the script an onEdit trigger? If so, you can add a trigger from the script editor that will run as yourself. Just make sure to rename onEdit() to something else so it doesn't try to run as the regular user as well.
If this isn't onEdit, I would suggest making the function they call add a specific value to a specific cell. Then you can have a timed trigger every minute running as yourself that watches this cell and does its work when the cell contains the specific word. Just make sure to clear the cell quickly so it doesn't run twice.