Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 - Fatal编程技术网

Google apps script 用于合并列并在另一列中自动更新的google工作表脚本

Google apps script 用于合并列并在另一列中自动更新的google工作表脚本,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我一直在写一个google sheet脚本来合并两列并自动更新列 DATE Time DateTime --------------------------------------------------------- 2020-03-31 1:00PM 2020-03-31 13:00:00 我有两栏“日期”和“时间”。手动输入(不能自动插入)。所以我想尝试一下,如果两列都被填充,那么另一列“Dat

我一直在写一个google sheet脚本来合并两列并自动更新列

DATE                Time              DateTime
---------------------------------------------------------
2020-03-31          1:00PM           2020-03-31 13:00:00
我有两栏“日期”和“时间”。手动输入(不能自动插入)。所以我想尝试一下,如果两列都被填充,那么另一列“DateTime”将被自动填充

DATE                Time              DateTime
---------------------------------------------------------
2020-03-31          1:00PM           2020-03-31 13:00:00
我使用了这个函数> =连接(文本(A2,“yyyy-mm-dd”)和“&text(B2,“hh:mm:ss”))

但我怎么能在脚本下写这个呢! 这是我的googlesheet链接:

以下是我的功能:

function onEdit(event)
{ 
  var timezone = "IST";
  var timestamp_format = "MM-dd-yyyy hh:mm:ss a"; // Timestamp Format. 
  var updateColName = "DATE";
  var updateColName2 = "Time";
  var timeStampColName = "DateTime";
  var sheet = event.source.getSheetByName('Sheet1'); //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); 
  var updateCol2 = headers[0].indexOf(updateColName2);

  updateCol = updateCol+1;
  updateCol2 = updateCol2+1;


  if (dateCol > -1 && index > 1 &&  editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
    if (dateCol > -1 && index > 1 && editColumn == updateCol2){

       var cell = sheet.getRange(index, dateCol + 1);
       var date = concatenate(text(A2,"yyyy-mm-dd")&" "&text(B2,"hh:mm:ss"))

       cell.setValue(date);
    }
  }
}

此功能不起作用。请任何人帮助我。

如果您需要将时间值添加到日期值,您只需要将它们添加到一起(而不是串联),因为除了格式之外,它们内部只是数字

=ARRAYFORMULA(IF((A:A <> "") * (B:B <> ""), A:A + B:B,))
=ARRAYFORMULA(如果((A:A“”)*(B:B“”),A:A+B:B,)


在列
D:D
中,我已将格式设置为日期时间

为什么需要脚本?您可以简单地使用and if语句查看日期和时间列中是否有任何条目,并使用连接公式。我在你的工作表上加了这个。请看一看,谢谢。工作很好。