Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 由公式触发的时间戳Google脚本_Google Apps Script_Google Sheets_Triggers_Timestamp - Fatal编程技术网

Google apps script 由公式触发的时间戳Google脚本

Google apps script 由公式触发的时间戳Google脚本,google-apps-script,google-sheets,triggers,timestamp,Google Apps Script,Google Sheets,Triggers,Timestamp,一旦集合列中的单元格被更新,我将使用此Google脚本在我的Google工作表的列中获取时间戳: `function activeSheetName() { return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName(); } function onEdit(event) { var timezone = "GMT+9"; var timestamp_format = "MM-dd-yyyy"; // Ti

一旦集合列中的单元格被更新,我将使用此Google脚本在我的Google工作表的列中获取时间戳:

`function activeSheetName() {
return
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
}
function onEdit(event)
{ 
  var timezone = "GMT+9";
  var timestamp_format = "MM-dd-yyyy"; // Timestamp Format. 
  var updateColName = "Profit";
  var timeStampColName = "Time";
  var sheet = event.source.getSheetByName(activeSheetName()); //Name of the sheet where you want to run this script.


  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
  if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
}`

当我手动更改触发器列中的值时,脚本会工作。但是,我在触发器列中使用了一个基于“触发器”的公式,它将在触发器列更改/更新时自动更新触发器列。问题是,此脚本不会响应在列中填充的触发数据,只响应手动输入的值。我该如何解决这个问题?

公式无法激活简单或可安装的触发器。为了解决这个问题,你必须重新思考你的解决方案

也许不是逐个单元格“读取”更改,而是读取通过编辑多个单元格区域所做的更改,例如用户粘贴数据或进行填充时所做的更改

或者,您应该设计一种方法来捕获要输入的数据和时间戳

对于某些用例,最简单的解决方案是使用Google Forms For other,您应该创建一个web应用程序

参考文献

相关的


重构代码,以便通过独立函数完成时间戳的编写。然后让
onEdit()
以及其他触发器调用该函数

function addTimestamp(range) {
  // add the timestamp
}

function onEdit(event) {
  // does something
  addTimestamp(range);
}

function yourOtherTrigger() {
  // does something
  addTimestamp(range);
}

您还应该查看。您对
event
的使用效率不高(尤其是
event.source.getSheetByName(activeSheetName())
event.source.getActiveRange()

触发器只响应用户操作或时间间隔。它们不会对其他触发输出作出响应。这在文档中有明确的描述,并且不会改变。这似乎是有道理的,但老实说,我不擅长HTML之外的编码。如何在我现有的代码中实现这一点?或者更确切地说,你会如何用你的建议来修改它?