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
Google apps script 如何在google电子表格中自动添加时间戳_Google Apps Script_Timestamp_Google Sheets - Fatal编程技术网

Google apps script 如何在google电子表格中自动添加时间戳

Google apps script 如何在google电子表格中自动添加时间戳,google-apps-script,timestamp,google-sheets,Google Apps Script,Timestamp,Google Sheets,我在谷歌电子表格中有一张表格,包含5个单元格,前3个单元格只包含单词,而后2个单元格包含时间,特别是时间戳 cell 1 = data cell 2 = data cell 3 = data cell 4 = time start cell 5 = time ended 现在,我想要的是,当向单元格1提供数据时,时间戳将自动出现在单元格4中。当向小区2和小区3提供数据时,时间戳将是小区5的新值 我的朋友给了我一个代码,应该粘贴在脚本编辑器中: function readRows() { v

我在谷歌电子表格中有一张表格,包含5个单元格,前3个单元格只包含单词,而后2个单元格包含时间,特别是时间戳

cell 1 = data
cell 2 = data
cell 3 = data
cell 4 = time start
cell 5 = time ended
现在,我想要的是,当向单元格1提供数据时,时间戳将自动出现在单元格4中。当向小区2和小区3提供数据时,时间戳将是小区5的新值

我的朋友给了我一个代码,应该粘贴在脚本编辑器中:

function readRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

for (var i = 0; i <= numRows - 1; i++) {
  var row = values[i];
  Logger.log(row);
 }
};
此代码粘贴在
=IF(B6=”“,“”,timestamp(B6))
单元格4中,此
=IF(D6=“”,“”,timestamp(C6&B6))
位于单元格5上。在他的例子中,它起作用了。但是当我把它复制到我的时,第4单元和第5单元中的输出是今天的日期,而不是时间

有人能帮我吗?为什么它输出的是日期而不是时间?

如果这有帮助的话,您可以参考这个。 在脚本代码中,更改

var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.


这可能会对您有所帮助。

我刚刚遇到这个问题,并修改了提供的代码

他们的代码通过更新指定列来工作,时间戳插入到另一个指定列的同一行中

我改变的是我将日期和时间分开,因为时间戳是字符串,而不是日期格式。我的方法对于生成图形很有用

它的工作原理是指定要跟踪更改的列,然后分别为日期和时间创建更新列和正常运行时间列

function onEdit(event) {
  var timezone = "GMT+1";
  var date_format = "MM/dd/yyyy";
  var time_format = "hh:mm";

  var updateColName = "Резултат";

  var DateColName = "upDate";
  var TimeColName = "upTime";

  var sheet = event.source.getActiveSheet(); // All sheets
  // var sheet = event.source.getSheetByName('Test'); //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(DateColName);
  var timeCol = headers[0].indexOf(TimeColName);

  var updateCol = headers[0].indexOf(updateColName);
  updateCol = updateCol + 1;

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

    var cellDate = sheet.getRange(index, dateCol + 1);
    var cellTime = sheet.getRange(index, timeCol + 1);

    var date = Utilities.formatDate(new Date(), timezone, date_format);
    var time = Utilities.formatDate(new Date(), timezone, time_format);

    cellDate.setValue(date);
    cellTime.setValue(time);
  }
}
希望这能帮助人们

更新和更简单的代码
我做了一个稍微不同的版本,也是基于

为了支持多个命名的工作表,并且由于GoogleSheets脚本目前不支持Array.prototype.includes(),我加入了前面提到的polyfill

此外,在我的版本中,时间戳标记了该行单元格的创建日期,而不是上次更新的日期,如这里提供的其他脚本所示

function onEdit(event) {
  var sheetNames = [
    'Pounds £', 
    'Euros €'
  ]
  var sheet = event.source.getActiveSheet();
  if (sheetNames.includes(sheet.getName())){
    var timezone = "GMT";
    var dateFormat = "MM/dd/yyyy";
    var updateColName = "Paid for ...";
    var dateColName = "Date";

    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf(dateColName) + 1;
    var updateCol = headers[0].indexOf(updateColName) + 1;
    var dateCell = sheet.getRange(rowIndex, dateCol);
    if (dateCol > 0 && rowIndex > 1 && editColumn == updateCol && dateCell.isBlank())
    {
      dateCell.setValue(Utilities.formatDate(new Date(), timezone, dateFormat));
    }
  }
}

// https://stackoverflow.com/a/51774307/349169
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      // 1. Let O be ? ToObject(this value).
      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }

      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;

      // 5. If n ≥ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
      }

      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        if (sameValueZero(o[k], searchElement)) {
          return true;
        }
        // c. Increase k by 1. 
        k++;
      }

      // 8. Return false
      return false;
    }
  });
}
函数onEdit(事件){
变量名称=[
“英镑”,
“欧元”
]
var sheet=event.source.getActiveSheet();
if(sheetNames.includes(sheet.getName())){
var timezone=“GMT”;
var dateFormat=“MM/dd/yyyy”;
var updateColName=“付费…”;
var dateColName=“日期”;
var actRng=sheet.getActiveRange();
var editColumn=actRng.getColumn();
var rowIndex=actRng.getRowIndex();
var headers=sheet.getRange(1,1,1,sheet.getLastColumn()).getValues();
var dateCol=headers[0].indexOf(dateColName)+1;
var updateCol=headers[0]。indexOf(updateColName)+1;
var dateCell=sheet.getRange(rowIndex,dateCol);
如果(dateCol>0&&rowIndex>1&&editColumn==updateCol&&dateCell.isBlank())
{
setValue(Utilities.formatDate(new Date(),timezone,dateFormat));
}
}
}
// https://stackoverflow.com/a/51774307/349169
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if(!Array.prototype.includes){
Object.defineProperty(Array.prototype,'includes'{
值:函数(searchElement,fromIndex){
if(this==null){
抛出新的TypeError(“'this'为null或未定义”);
}
//1.设O为?ToObject(该值)。
var o=对象(此);
//2.设len为?ToLength(?Get(O,“length”))。
var len=o.length>>>0;
//3.如果len为0,则返回false。
如果(len==0){
返回false;
}
//4.设n为?ToInteger(fromIndex)。
//(如果未定义fromIndex,此步骤将生成值0。)
var n=fromIndex | 0;
//5.如果n≥ 0,那么
//让k为n。
//6.否则n<0,
//让k是len+n。
//b.如果k<0,则k为0。
var k=Math.max(n>=0?n:len-Math.abs(n),0);
函数sameValueZero(x,y){
返回x==y | |(x的类型=='number'&y的类型=='number'&isNaN(x)&isNaN(y));
}
//7.当k
@PaulG-即使没有链接,答案仍然包含一些答案。@PaulG你是对的,但我不想因为最初的开发人员花费时间和精力来记录和编码这些东西而失去荣誉。所以在这里,我只是给提问者提供了正确的方向。当然,有时链接可能不起作用,在这种情况下,应该有人再次提问/回答问题。可能重复
function onEdit(event) {
  var timezone = "GMT+1";
  var date_format = "MM/dd/yyyy";
  var time_format = "hh:mm";

  var updateColName = "Резултат";

  var DateColName = "upDate";
  var TimeColName = "upTime";

  var sheet = event.source.getActiveSheet(); // All sheets
  // var sheet = event.source.getSheetByName('Test'); //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(DateColName);
  var timeCol = headers[0].indexOf(TimeColName);

  var updateCol = headers[0].indexOf(updateColName);
  updateCol = updateCol + 1;

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

    var cellDate = sheet.getRange(index, dateCol + 1);
    var cellTime = sheet.getRange(index, timeCol + 1);

    var date = Utilities.formatDate(new Date(), timezone, date_format);
    var time = Utilities.formatDate(new Date(), timezone, time_format);

    cellDate.setValue(date);
    cellTime.setValue(time);
  }
}
function onEdit(e) {
  var sh = e.source.getActiveSheet();
  var sheets = ['Sheet1']; // Which sheets to run the code.

  // Columns with the data to be tracked. 1 = A, 2 = B...
  var ind = [1, 2, 3].indexOf(e.range.columnStart); 

  // Which columns to have the timestamp, related to the data cells.
  // Data in 1 (A) will have the timestamp in 4 (D)
  var stampCols = [4, 5, 6]

  if(sheets.indexOf(sh.getName()) == -1 || ind == -1) return;

  // Insert/Update the timestamp.
  var timestampCell = sh.getRange(e.range.rowStart, stampCols[ind]);
  timestampCell.setValue(typeof e.value == 'object' ? null : new Date());
}
function onEdit(event) {
  var sheetNames = [
    'Pounds £', 
    'Euros €'
  ]
  var sheet = event.source.getActiveSheet();
  if (sheetNames.includes(sheet.getName())){
    var timezone = "GMT";
    var dateFormat = "MM/dd/yyyy";
    var updateColName = "Paid for ...";
    var dateColName = "Date";

    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf(dateColName) + 1;
    var updateCol = headers[0].indexOf(updateColName) + 1;
    var dateCell = sheet.getRange(rowIndex, dateCol);
    if (dateCol > 0 && rowIndex > 1 && editColumn == updateCol && dateCell.isBlank())
    {
      dateCell.setValue(Utilities.formatDate(new Date(), timezone, dateFormat));
    }
  }
}

// https://stackoverflow.com/a/51774307/349169
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      // 1. Let O be ? ToObject(this value).
      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }

      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;

      // 5. If n ≥ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
      }

      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        if (sameValueZero(o[k], searchElement)) {
          return true;
        }
        // c. Increase k by 1. 
        k++;
      }

      // 8. Return false
      return false;
    }
  });
}