【问题标题】:Google Apps Script - Copy Paste values on same page and in same row based on cell criteriaGoogle Apps 脚本 - 根据单元格条件在同一页和同一行中复制粘贴值
【发布时间】:2023-01-21 06:48:44
【问题描述】:

我有一个表,其中列 A:Ak 中有数据。一些数据包含公式,当一行数据完成时(即 W 列中的状态为“Y”),我想复制该行并仅复制过去的值。

这是我试过的:

function Optimize() {
  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];
    // This searches all cells in columns W copies and pastes values in row if cell has value 'Y'
    if (row[22] == 'Y') {
    sheet.getDataRange(sheet.getCurrentCell().getRow() - 0, 1, 1, sheet.getMaxColumns()).activate();
    sheet.getActiveRange().copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
    rowsCopiedpasted++;
    }
  }
}

当我在工作表中并单击具有“Y”值的行的 A 列中的单元格时,这有效,但我需要它遍历整个工作表并复制/粘贴列中具有“Y”的所有行的值W。

【问题讨论】:

    标签: for-loop google-apps-script google-sheets copy-paste


    【解决方案1】:

    尝试这个:

    function Optimize() {
      const sh = SpreadsheetApp.getActiveSheet();
      const rg = sh.getDataRange();
      const vs = rg.getValues(); 
      const lc = sh.getLastColumn();
      let o = vs.forEach((r,i) => {
        if(r[22] == "Y") {
          sh.getRange(i + 1, lc + 1,1,lc).setValues([r])
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多