Reputation: 61
I have a script to copy data from one google sheet to another, the code is this:
function copy() {
var sss = SpreadsheetApp.openById('ID'); //replace with source ID
var ss = sss.getSheetByName('Cap'); //replace with source Sheet tab name
var range = ss.getRange('A:F'); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('ID'); //replace with destination ID
var ts = tss.getSheetByName('Cap'); //replace with destination Sheet tab name
ts.getRange(1, 1, data.length, data[0].length).setValues(data);
}
I need to run this script everyday at 12:00am, and until 12:30am run it every minute, I don't need it to run anymore on the day
I can set triggers like daytime (to run between 12 and 1 am) or hour time (every hour) but thats not what I need, thats not accurate, I just need to run exactly at 12:00am and so on as explained for 30 minutes every minute
and if I set the trigger to run every minute I get "Service using too much computer time for one day"
Is there a way to set this on the script trigger ?
Thanks !
Upvotes: 2
Views: 4839
Reputation: 61
FYI
I found the solution, if anybody needs the same
the code is
function CopyLive1() {
var date = new Date();
var day = date.getDay();
var hrs = date.getHours();
var min = date.getMinutes();
if ((hrs >= 0) && (hrs <= 0) && (min >= 0) && (min <= 30 )) {
var sss = SpreadsheetApp.openById('ID'); //replace with source ID
var ss = sss.getSheetByName('L1'); //replace with source Sheet tab name
var range = ss.getRange('A:E'); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('ID'); //replace with destination ID
var ts = tss.getSheetByName('L1'); //replace with destination Sheet tab name
ts.getRange(1, 1, data.length, data[0].length).setValues(data);
}
}
and just add the every minute trigger, this way will only run every minute between 12:00am and 12:30am
Upvotes: 4
Reputation: 2286
There are limitations that Google places on how often you can use triggers. You cannot set a bunch of triggers for every minute as you are limited to 20 per user (more on quotas here). As far as I know this is for concurrent triggers. A trigger on a daily repeat will only fire randomly every hour.
You may want to try to create triggers programmatically. You would set an initial trigger, which would fire the script at the specific day and time.
The script would have to have an additional function that would check what time it is and would accordingly delete the previous trigger, create a trigger for 12:01 then 12:02 then 12:03 and so on, until it checks that the trigger created was at 12:30 at which point, the next trigger it creates is for the next day at 12:00.
The key problem here is to be absolutely certain that you script never takes more than 1 minute to execute and that it definitely starts in time. You can also just try to have it use the repeat every minute trigger, but the same applies — your script cannot run for more than 1 minute or you will have problems firing it the next time.
As for deleting the old trigger, you can do something like this easily:
function(trig) {
var triggers;
var trigCount;
triggers = ScriptApp.getProjectTriggers();
for (trigCount = 0; trigCount<triggers.length; trigCount++) {
if (triggers[trigCount].getUniqueId() == trig.triggerUid) {
ScriptApp.deleteTrigger(triggers[trigCount]);
break;
}
}
}
And that will delete whatever trigger started the script.
Upvotes: 1