如何根据单元格值查找特定行并将当前日期插入该行的单元格

时间:2018-07-16 05:39:55

标签: google-apps-script timestamp find

我有一个包含数百行的工作表。 第一列包括参考编号,该参考编号对于每一行都是唯一的。 我想找到参考号为特定数字(例如301)的行,然后将时间戳(日期和时间)插入到列M中该行的单元格中。

我有以下代码,该代码尚未完成,将无法正常工作。

var spreadsheet = SpreadsheetApp.getActive();
var RefNumber = 301 //for example
var searchRange = spreadsheet.getSheetByName('Classes').getRange("A:A");
// get the values in an array
var values = searchRange.getValues();
// examine the values in the array
var i = []; 
   for (var y = 0; y < values.length; y++) {
    if(values[y] == RefNumber){
      i.push(y);
     }
     }
   // I have no idea how to continue the code! Please include your suggestions here.

1 个答案:

答案 0 :(得分:1)

您要从A列中搜索一个号码,并将时间戳记与找到搜索到的号码的行相同的M列。如果我的理解是正确的,那么该修改如何?

修改点:

  • getValues()检索的值是二维数组。
  • 通过使用getLastRow(),费用可以低于“ A:A”的费用。
  • RefNumber中找到values时,它将在i中放入a1表示法。这样,可以使用getRangeList()放置时间戳。

修改后的脚本:

var spreadsheet = SpreadsheetApp.getActive();
var RefNumber = 301 //for example
var sheet = spreadsheet.getSheetByName('Classes'); // Modified
var searchRange = sheet.getRange("A1:A" + sheet.getLastRow()); // Modified
// get the values in an array
var values = searchRange.getValues();
// examine the values in the array
var i = [];
for (var y = 0; y < values.length; y++) {
  if(values[y][0] == RefNumber){
    i.push("M" + (y + 1)); // Modified
  }
}
// I have no idea how to continue the code! Please include your suggestions here.
sheet.getRangeList(i).setValue(new Date()); // Added

参考文献:

如果我误解了你的问题,对不起。那时,您可以提供所需的样本输入和输出吗?我想修改脚本。