【问题标题】:Add timestamp in a different sheet when cell equals a specific value with matching ID #'s当单元格等于具有匹配 ID # 的特定值时,在不同的工作表中添加时间戳
【发布时间】:2018-01-05 20:09:51
【问题描述】:

我正在使用 google 工作表,并且有一个名为 Sales 的工作表存储我的当月销售数据,另一个工作表使用索引(匹配)提取库存数据,所有这些数据都基于 Item#。我想用“是”列 L 标记已售出的产品。一旦我选择“是”,我希望脚本将已售日期输入到已售商品的库存表中。我觉得我很接近,但我只有去相邻单元格的时间戳,我需要它来查找 ID 号并将时间戳留在已售产品的库存表中。注意:每月复制一份销售表,将其重命名为“Sales for XXX”,然后将“销售”表中的数据擦除干净,这样我需要粘贴时间戳,并且在下个月的数据清除后不会被删除.

function onEdit(event){ 
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "SALES" ) { //checks that we're on the correct sheet
  var ColL = 12;  // Column Number of "L"
  var changedRange = event.source.getActiveRange();
  if (changedRange.getColumn() == ColL) {
    // An edit has occurred in Column L
    var state = changedRange.getValue();
    var adjacent = 
event.source.getActiveSheet().getRange(changedRange.getRow(),ColL+1);
    var timestamp = new Date(); // Get the current time
    // We'll do something different, depending on the selected value
    switch (state) {
      case "Yes":
        // Write timestamp into adjacent cell
        adjacent.setValue(timestamp);
        break;
      case "No":
        // Erase timestamp in adjacent cell
        adjacent.clearContent();
        break;
   }
  }
 }
}

【问题讨论】:

    标签: google-apps-script google-sheets timestamp


    【解决方案1】:

    可能有一种简单的方法可以只用公式来做到这一点。 带有 if 语句。我没有从单独的表格中提取数字,因为我不确定您是如何实现的。

    At any rate here is a demo on a single sheet

    这是我使用的代码。

    function onEdit(event){ 
      var s = SpreadsheetApp.getActiveSheet();
      if( s.getName() == "SALES" ) { //checks that we're on the correct sheet
        var currCell = s.getActiveCell()
        var ajCell = s.getRange(currCell.getRow(), currCell.getColumn()+1)
        if(currCell.getValue() === "YES"){
          ajCell.setValue(new Date())
        }else{
         ajCell.setValue("")
        }                         
      }
    }
    

    【讨论】:

      【解决方案2】:

      我不确定我是否理解您最后说明的要求,但看起来您确实很接近。要在 Apps 脚本中查找和操作工作表中的值,您可以使用范围 getValues() 和 setValues() 方法将工作表数据作为二维数组进行处理。例如,下面是一个函数,它以时间戳和 ID 作为参数,并在名为“INVENTORY”的工作表中找到第一列中具有 ID 的对应行,并将时间戳写入第二列。您需要针对工作表布局对其进行调整,但这应该可以帮助您朝着正确的方向前进。

      此函数在每次运行时都会重写 INVENTORY 表。如果这太过分了,您可以使用 array.forEach 函数来查找 ID 匹配的行。 forEach 回调中的第二个参数是被评估元素的索引,可用于返回匹配行的行号。

      /*
        Assumes a sheet named INVENTORY with IDs in the first column and timestamps in 
        the second column, with column headers. The column headers help ensure that the 
        getValues and setValues arrays have the same dimensions.
      */
      function setSoldTimestamp(timestamp,id){
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName('INVENTORY'); // change to your inventory sheet name
        var range = sheet.getDataRange();
        var arSheet = range.getValues();
        // there are a lot of different ways you could iterate through
        // the array to find what you are looking for. I like map and filter.
        var arOut = arSheet.map(function(row){
          if (row[0] == id) { // change index to match your ID column number - 1
            row[1] = timestamp; // change index to match your timestamp column number - 1
            return row
          } else {
            return row
          }
        });
        range.setValues(arOut);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 2016-09-12
        • 1970-01-01
        相关资源
        最近更新 更多