将动态范围从一个电子表格复制到另一个使用getfileid

时间:2019-02-26 17:26:48

标签: spreadsheet appscript

my sample spreadsheet,我的代码需要帮助。

任务:遍历“客户”中的“ A”列,并获得相应的工作表名称。获取工作表并将工作表中所有使用的范围(例如(A; B; C; etc))复制到B列中引用的特定文件。就是这样-很简单。好吧,关于应用程序脚本,我就像小树苗一样绿。我整个周末和昨天都在这里度过(非常有趣)。

我到目前为止的努力

function updatefile() {
   var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheetByName('CUSTOMER')
   var data = sh.getRange("A2:B12").getValues(); // get the range of non emmpty cells
     for (var i = 1; i < data.length; i++) {
    var source = ss.getSheetByName(data[i][0]);
   var range = source.getRange('C3:M1003'); //assign the range you want to copy
    var data = range.getValues();

   var destination = SpreadsheetApp.openById(data[i][1]); // open the 
    coresponding file by using the file id in the secound column 
     var destbodya = destination.getSheetByName('UPDATE ON ORDERS');
      destbodya.getRange(ts.getLastRow()+1, 1,49,7).setValues(data); //you will need to define the size of the copied data see getRange()

} }

1 个答案:

答案 0 :(得分:0)

经过大量的搜索和试验,我终于可以使用它了。对于那些面临这些挑战的人。干杯

function updatefile() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('CUSTOMER')
  var data = sh.getRange("A2:B73").getValues(); // get the range of non emmpty cells
  for (var i = 1; i < data.length; i++) {
    if (data[i][0] != '') {
      var source = ss.getSheetByName(data[i][0]);
      var range = source.getRange('C3:M1003'); //assign the range you want to copy
      var valuedata = range.getValues();

      //var destination = SpreadsheetApp.openById("xxxxxxxxxxxxrrrrrsomeID"); // enter file ID directly or the nest line
      var destination = SpreadsheetApp.openById(data[i][1]); // Iterate through the range and open the corresponding file by using the file id in the secound column 
      var dest_1body = destination.getSheetByName('blah blah sheetname'); // first sheet destination to copy data to
      dest_1body.getRange("A2:K1002").setValues(valuedata); //you will need to define the size of the copied data see getRange()

      var destination = SpreadsheetApp.openById(data[i][1]); // open the coresponding file by using the file id in the secound column
      var dest_2body = destination.getSheetByName('blah blah sheetname'); // second sheet to copy same data to

      dest_2body.getRange("A28:K1028").setValues(valuedata); //you will need to define the size of the copied data see getRange()
    }
  }
}