Google表格将行复制到不同的电子表格

时间:2015-09-03 10:46:48

标签: google-apps-script google-sheets

我把这个测试放在一起:

https://docs.google.com/a/strandvinduspuss.no/spreadsheets/d/1WZTYq-WiQq8asXZjHMMN9bsGiQKbPKmlRrsnkAsadU4/edit?usp=sharing

功能:在列表选项卡中,当您更改col B的名称时,该行的一部分将被复制到匹配的工作表。

我需要的是将它们复制到单独的电子表格中(每个雇主一个)。这可能吗?

我的代码:

// http://victorwyee.com/gapps/move-row-on-cell-value-google-spreadsheets/
   /**
    * Moves row of data to another spreadsheet based on criteria in column 6.
    * Each spreadsheet can be a "source" spreadsheet, or the "target"    spreadsheet.
    * Assumes there as many spreadsheets as there are criteria values, e.g.
    *   if the criteria are "R", "P", and "D", then there are three spreadsheets
    *   named "R", "P" and "D".
    * Assumes that each spreadsheet has the same structure.
    */

  function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var r = SpreadsheetApp.getActiveRange();

  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();

  // Get the number of columns in the active sheet.
  var colNumber = s.getLastColumn();

   // Move row based on criteria in column 6, and if row is not the header.
  if (colIndex == 2 && rowIndex != 1) {

    // Get value from column 6, in the active row.
    var status = s.getRange(rowIndex, colIndex).getValue();

    // Do nothing if criteria value is not actually changed to something else.
    if (s.getName() != status) { 

      // The target sheet is the one with the same name as the criteria value.
      var targetSheet = ss.getSheetByName(status);
      var target = targetSheet.getRange(targetSheet.getLastRow() +1, 1);

      // Set the range and copy
      // s.getRange(rowIndex, 10, 1, colNumber).copyTo(target);
      targetSheet.insertRowsAfter(targetSheet.getLastRow(), 1);
      s.getRange(rowIndex, 1, 1, 16).copyTo(target);

      // s.deleteRow(rowIndex);
    }
  }
}

进行了一些更改,因此评论不会更新。

2 个答案:

答案 0 :(得分:0)

您可以将工作表复制到其他电子表格,但(据我所知,根据我收到的文档和错误)您无法将范围复制到单独的电子表格中。我有一个类似的用例,我的解决方法是将我想要复制的内容移动到单独的工作表,然后将该工作表复制到新的/其他电子表格中:

    cd qt_source_directory
    mkdir my_build
    cd my_build
    ../configure \
      -release \
      -opensource \
      -no-compile-examples \
      -platform linux-g++-64 \
      -xplatform win32-g++ \
      -device-option CROSS_COMPILE=/usr/bin/x86_64-w64-mingw32- \
      -skip qtactiveqt \
      -v

如果您需要保留相同的工作表/数据来移动数据,那么这对您的情况不起作用,因为它会被覆盖。在这种情况下,一个选项是复制工作表,然后移动您想要的任何数据,然后删除多余的工作表。

var newSpreadsheet = SpreadsheetApp.create("I am a copy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Copy Me");
sheet.copyTo(newSpreadsheet);

希望这有助于您朝着正确的方向前进......

答案 1 :(得分:0)

我认为copyTo在不同的电子表格之间不起作用。您可以使用getValuesetValue复制信息。

而不是s.getRange(rowIndex, 1, 1, 16).copyTo(target);尝试:



var values = s.getRange(rowIndex, 1, 1, 16).getValue();
targetSheet.getRange(target).setValue(values);




有关详情,请查看range documentation page,其中有setValuegetValue的解释。