使用 App 脚本在 google sheet 中条件匹配时复制行数据并粘贴到另一个 sheet

Copy the row data and Paste to another sheet when criteria match in google sheet with App Script

我正在进行 Fleet 维护 google sheet,因为当条件匹配时,我需要将数据复制并粘贴到另一个 sheet。

当数据中的服务状态 sheet 等于“服务预警报”或“服务警报”或“已过期”时,该行应复制并粘贴到带有选定列的输出 sheet和日期戳。使用应用程序脚本是可能的。

注意:数据sheet,每当HMR读数更新时,服务状态列将自动更新,因此它会重复处理。

传播sheetLink: https://docs.google.com/spreadsheets/d/1UkPGyBFTXUAtlpGmdEDLYo0p1G-tAazjjCFMsu2FqgM/edit?usp=sharing

提前致谢

约翰

试试这个

function alert2() {
  // with integration of the extra column
  var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Output2");
  var output = []
  if (sh1.getLastRow() == 1) { return }
  if (sh2.getLastRow() > 2) { 
    output = sh2.getRange(2, 1, sh2.getLastRow() - 1, sh2.getLastColumn()).getValues().filter(e => (e[12] == "Completed"))
    sh2.getRange('A2:L'+(sh2.getLastRow())).clearContent()
  }
  var data = sh1.getRange(2, 1, sh1.getLastRow() - 1, sh1.getLastColumn()).getValues().filter(e => (e[14] == "Expired" || e[14] == "Service Alert" || e[14] == "Service Pre Alert"))
  try {
    data.forEach(function(r){
      var alertData = []
      r.forEach((c, col) => {
        if (col<=9 || col==14) alertData.push(c);
      })
      alertData.push(new Date())
      var workSatus = r[14] == 'Expired' ? 'Pending' : 'Service due'
      alertData.push(workSatus)
      output.push(alertData)
    });
  } catch (e) { }
  sh2.getRange(2,1,output.length,output[0].length).setValues(output)
}