简体   繁体   English

基于时间的Google脚本-每分钟12:00 am至12:30 am触发

[英]Time based Google Script - Trigger between 12:00am to 12:30am every minute

I have a script to copy data from one google sheet to another, the code is this: 我有一个脚本可以将数据从一个Google工作表复制到另一个工作表,代码是这样的:

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 我需要每天凌晨12:00运行此脚本,直到凌晨12:30每分钟运行一次,我不再需要在当天运行该脚本

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 我可以设置触发器,例如白天(在12点至凌晨1点之间运行)或小时时间(每隔一小时),但这不是我所需要的,那不准确,我只需要在12:00 am准确运行,依此类推,如30每分钟

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 ! 谢谢 !

There are limitations that Google places on how often you can use triggers. Google对触发器的使用频率有一些限制。 You cannot set a bunch of triggers for every minute as you are limited to 20 per user (more on quotas here ). 您不能为每分钟设置一堆触发器,因为每个用户最多只能设置20个触发器( 此处更多关于配额的信息 )。 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. 该脚本将必须具有一个附加功能,该功能将检查现在几点,并相应地删除先前的触发器,为12:01然后12:02然后12:03创建触发器,依此类推,直到检查触发器是否已创建当时是12:30,那么它创建的下一个触发条件是第二天的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. 这里的关键问题是要绝对确定您的脚本执行时间不会超过1分钟,并且肯定会及时启动。 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. 您也可以尝试让它使用每分钟重复一次触发器,但是同样适用-您的脚本不能运行超过1分钟,否则您下次启动时会遇到问题。

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. 这将删除启动脚本的任何触发器。

FYI 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 并添加每分钟触发,这种方式只会在12:00 am和12:30 am之间每分钟运行一次

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从上午 9 点到下午 12 点,每 30 分钟运行一次脚本,使用触发器 - Run script every 30 minutes from 9 am to 12 pm with triggers 每天 8:00、12:30、17:00 运行 Gmail Google Apps 脚本 - Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00 为什么Google表格中的日期验证会拒绝今天上午8:00之前的日期? - Why date validation in Google sheets rejects today's date before 8:00am? Google脚本每30天触发一次 - Google Script Trigger Every 30 Days 仅在工作日每小时,在特定时间之间运行 Google 脚本 - 使用基于时间的触发器? - Run a Google Script only weekdays every hour, between certain hours - using time-based trigger? 类 ClockTriggerBuilder:特定日期和时间(仅一次)Google Sheet Script 中的触发器每分钟重复执行 - Class ClockTriggerBuilder: Specific Date and Time (only once) Trigger in Google Sheet Script keeps repeatedly executing every minute 我想制作一个谷歌脚本,每分钟将运行总数加 1,永远。 利用以前的帖子,我觉得我很接近 - I want to make a google script that adds 1 to a running total every minute, in perpetuity. Utilizing previous posts I feel I am close Google Apps 脚本应仅在工作日上午 10 点至下午 5 点每小时触发 - Google Apps Script should only trigger during week days during hours 10 AM - 5 PM every hour 时间驱动触发器:是否可以每分钟为部署脚本应用程序/gmail 附加组件设置一个触发器? - Time-driven Trigger : Possibility to set a trigger up every minute for a deploy script app / gmail add-on? Google Apps 脚本 - 基于时间的触发器 - Google Apps Script - Time Based Trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM