Google Apps 脚本将带有注释的行移动到另一个工作表

问题描述 投票:0回答:0

我写了一个脚本来将行移动到另一个工作表,它可以工作但不完全符合我的意愿。

我有一个带有 2 个选项卡的 SpreadSheet,一个是“A PLANFIER”,一个是“MASTER”。 在选项卡“A PLANIFIER”中,B 列中的值可以是 OUI 或 NON,要移动的行是值为 OUI 的行。 列 B、D、F 到 U、V 到 AB 和 AE 从“A PLANFIER”到 H、J、L 到 AA、AM 到 AS 和 BL 分别从“MASTER”。 我使用 moveTo() 函数,因为它会移动单元格中的音符。

当我运行它时,即使它应该移动几行,它也只移动了一行。所以我必须运行它几次。

如果你看到改进我的脚本的解决方案,你能告诉我吗,它也有点慢,所以如果你认为其他功能会运行得更快。

我的表格

我的剧本:

function transferData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName("A PLANIFIER");
  var targetSheet = ss.getSheetByName("MASTER");
  var lastRowSource = sourceSheet.getLastRow();
  var lastRowTarget = targetSheet.getLastRow();

  //Loop through the source data starting from row 6
  for (var i = 6; i <= lastRowSource; i++) {
    //Check if the value in column B is "OUI"
    if (sourceSheet.getRange("B" + i).getValue() == "OUI") {
      //Find the last row with a value in column M of the target sheet
      for (var j = lastRowTarget; j > 0; j--) {
        if (targetSheet.getRange("M" + j).getValue() != "") {
          var targetRow = j + 1;
          break;
        }
      }
      //Copy and paste values
      sourceSheet.getRange("B" + i).moveTo(targetSheet.getRange("H" + targetRow));
      sourceSheet.getRange("D" + i).moveTo(targetSheet.getRange("J" + targetRow));
      sourceSheet.getRange("F" + i + ":U" + i).moveTo(targetSheet.getRange("L" + targetRow + ":AA" + targetRow));
      sourceSheet.getRange("V" + i + ":AB" + i).moveTo(targetSheet.getRange("AM" + targetRow + ":AS" + targetRow));
      sourceSheet.getRange("AE" + i).moveTo(targetSheet.getRange("BL" + targetRow));

    //Increment the target row
    lastRowTarget++;

    // Keep the format of the source line
    var rng = sourceSheet.getRange("A50:AG50")
    rng.copyTo(sourceSheet.getRange("A6:AG50" + i), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
    break;
   }
 }
  sortM()
  sortAP()
}
javascript google-apps-script google-sheets row
© www.soinside.com 2019 - 2024. All rights reserved.