[英]Google Apps Script not calling function on time based Condition
I have a Apps Script function named UpdateDatabase
我有一个名为
UpdateDatabase
的 Apps 脚本 function
And called it by a if statement
并通过
if statement
调用它
But the function was being not called when the condition was met.但是在满足条件时没有调用 function。
What went wrong or how to call the function on a Particular time
(Hour & Minutes)..?出了什么问题或如何在
Particular time
(小时和分钟)拨打 function..?
Code.gs代码.gs
function UpdateDatabase() {
ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName("SCRIPT");
data = sheet.getRange("A2").getValue() ;
server = "give.herosite.pro";
port = '3306';
dbName = "oncjsznl_wp921";
username = "oncjsznl_wp921";
password = "xxxxxxxxx";
url = "jdbc:mysql://" + server + ":" + port + "/" + dbName + "?characterEncoding=UTF-8";
conn = Jdbc.getConnection(url, username, password);
stmt = conn.createStatement();
stmt.execute("UPDATE `my_table` SET `Script`='" + data + "' ;");
console.log("DB Table Updated successfully")
conn.close();
}
var now = new Date().getTime() ;
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var date = d.getDate();
var ctdr = new Date(year, month, date, 16, 10, 00).getTime();
distance = ctdr - now;
if (distance < 0) {
UpdateDatabase() ;
}
It seems that you want to run the UpdateDabase function every day at 16:10, more or less.似乎你想在每天 16:10 或多或少运行 UpdateDabase function。
Instead of hardcoding that, you can create a trigger and schedule the time to run any function.您可以创建一个触发器并安排时间运行任何 function,而不是对其进行硬编码。
The "Select event source" field must be time-driven in order to set the schedule. “选择事件源”字段必须是时间驱动的,以便设置计划。
This is the right approach to achieve your purpose.这是实现您的目的的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.