简体   繁体   English

Google Apps 脚本未按时调用 function

[英]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.

相关问题 在Google Apps脚本中调用函数 - Calling a function in Google Apps Script Google Apps 脚本(电子表格) - 根据单元格中的条件选择电子表格中的数组 - Google Apps Script (Spreadsheet) - selecting an array in spreadsheet based on a condition in cells 在 Google Apps Script 中的函数内调用函数(invoke) - Calling a function within a function (invoke) in Google Apps Script 谷歌文档应用程序脚本 function 调用非常慢 - google docs apps script function calling very slow Google Apps Script - 从自定义菜单调用自定义函数 - 非嵌套 - Google Apps Script - Calling custom function from a custom menu - Not nested 根据variabel Google Apps脚本日历/电子表格中的值执行功能 - Do a function based on a value in a variabel Google Apps script calendar/spreadsheet 根据电子表格中的值在自定义日期和时间触发基于 Google Apps 脚本时间的触发器 - Google Apps Script Time based trigger at custom date and time based on value in spreadsheet 如果条件满足,则 Google Apps 脚本复制范围 - Google Apps Script Copy Range If Condition Met Google Apps脚本-使用for循环根据条件从范围中提取数据 - Google apps script - using for loop to pull data from range based on condition 不是 Google Apps 脚本中的 function 错误 - is not a function error in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM