简体   繁体   English

Google表格应用脚本时间戳记条件

[英]Google Sheet App Script Timestamp Condition(s)

This is probably really simple but I'm not able to figure this out. 这可能真的很简单,但我无法弄清楚。

I have a timestamp script that i copied from this cool dude 我有一个时间戳脚本,是我从这个帅哥那里复制的

Now, what I want to do is only create a timestamp if a cells in Columns A,B,C are changed or are filled. 现在,我想要做的是在列A,B,C一个细胞被改变或充满创建一个时间戳。

  /**
    * Creates a Date Stamp if a column is edited.
    */

//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = [2,3];
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Sheet you are working on
var SHEETNAME = 'Sheet1'



function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //checks that we're on the correct sheet.
  if( sheet.getSheetName() == SHEETNAME ) { 
    var selectedCell = ss.getActiveCell();
    //checks the column to ensure it is on the one we want to cause the date to appear.
    if( selectedCell.getColumn() == COLUMNTOCHECK[0,1]) { 
      var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
      dateTimeCell.setValue(new Date());
      }
  }
}

I had assumed that adding on a clause to the if statement with the var indexed would fix this.. with changing COLUMNTOCHECK to [2,3,4,5] and so forth. 我以为在if语句上添加一个带有var索引的子句将解决此问题,将COLUMNTOCHECK更改为[2,3,4,5]等。

if( selectedCell.getColumn() == COLUMNTOCHECK[0] || selectedCell.getColumn() == COLUMNTOCHECK[1] )

EDIT. 编辑。

Sorry, my apologies for my bad use of English and unclear request. 抱歉,我对英语使用不当和要求不明确表示歉意。

What I want is the following. 我想要的是以下内容。

  1. a user will enter data into my spreadsheet. 用户会将数据输入我的电子表格。
  2. I want to create a timestamp using the script above which only enters the timestamp when multiple cells are changed. 我想使用上面的脚本创建一个时间戳,该脚本仅在更改多个单元格时才输入时间戳。

so as an example 举个例子

John enters sales data into Cells A2, B2 = Timestamp Created. John将销售数据输入到单元格A2,B2 =已创建时间戳。

John enters data into Cells A2 = No Timestamp created. John将数据输入到单元格A2 =未创建时间戳。

Hopefully that makes sense? 希望这有意义吗?

  1. Use the event object to get your range. 使用事件对象获取您的范围。
  2. Use indexOf() on the array of COLUMNTOCHECK to check if one of the columns you're watching was edited. COLUMNTOCHECK数组上使用indexOf()来检查您正在查看的列之一是否已被编辑。
  3. Loop through COLUMNTOCHECK and check the values for each column in the edited row. 遍历COLUMNTOCHECK并检查已编辑行中每一列的值。 If each column in that row has a value, then enter the timestamp. 如果该行中的每一列都有一个值,则输入时间戳。

Depending on your requirements, you could simplify and make step 3 run faster. 根据您的要求,您可以简化并使第3步运行更快。 If you're only ever going to check columns A & B, you could simply grab that range once ( editedSheet.getRange(editedRow, 1, 1, 2).getValues() ) and then iterate through the returned array of values. 如果仅检查A和B列,则只需获取一次该范围( editedSheet.getRange(editedRow, 1, 1, 2).getValues() ),然后遍历返回的值数组。

Note also that I removed the offset and am placing the timestamp in column C. Again, I'm not sure of your requirements, but the implementation you had would have overwritten values in column B for edits made in column A. 还要注意,我删除了偏移量并将时间戳记放置在C列中。同样,我不确定您的要求,但是对于在A列中进行的编辑,您的实现将在B列中覆盖值。

function onEdit(e) {
  var COLUMN_TO_CHECK = [1,2]; // The columns to check if something is entered. (Columns A & B)
  var DATE_TIME_COLUMN = 3; // Where you want the date time stamp (Column C)
  var SHEET_NAME = "Sheet1"; // Sheet you are working on

  var editedCell = e.range;
  var editedSheet = e.range.getSheet();
  if (editedSheet.getName() === SHEET_NAME) {
    if (COLUMN_TO_CHECK.indexOf(editedCell.getColumn()) != -1) {
      var allFilled = true;
      var editedRow = editedCell.getRow();
      for (var i in COLUMN_TO_CHECK) {
        if (editedSheet.getRange(editedRow, COLUMN_TO_CHECK[i]).getValue() == "") {
          allFilled = false;
        }
      }
      if (allFilled) {
        editedSheet.getRange(editedRow,DATE_TIME_COLUMN).setValue(new Date());
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Script - Google-Sheet 行未按预期按时间戳排序 - Google App Script - Google-Sheet rows are not sorting by timestamp as expected Google Sheet App Script:将 1 个值从一张纸匹配到另一张纸,然后如果条件满足设置背景 - Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met set background Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 从工作表 A 复制粘贴到工作表 B 的最后一行谷歌应用程序脚本 - Copy fro Sheet A to paste in Sheet B's last row google app script 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script 如何在 App 脚本中循环 Google 表格公式 - How To Loop Google Sheet Formula In App Script 应用脚本新的 Google 表格创建问题 - App Script new Google Sheet creation issue Google App 脚本不适用于完整工作表 - Google App script not working for full sheet Google App Script - 导出没有工作表名称的活动工作表 - Google App Script - Export Active Sheet Without Sheet Name 在 Google App Script 中根据条件禁用按钮 - Disable a button upon condition in Google App Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM