从多个电子表格中收集数据并将其复制到另一个工作表中

时间:2014-02-05 22:39:25

标签: google-apps-script google-sheets

我想将数据从一个电子表格复制到另一个电子表格中。但 1.数据在几张纸上 2.只有某些列(只是为了加快速度,不需要所有列)

一开始我使用了以下脚本:

function CopyDataToNewFile() {
  var sss = SpreadsheetApp.openById('0AjN7uZG....'); // sss = source spreadsheet
  var ss = sss.getSheetByName('Monthly'); // ss = source sheet
  //Get full range of data
  var SRange = ss.getDataRange();
  //get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  //get the data values in range
  var SData = SRange.getValues();

  var tss = SpreadsheetApp.openById('8AjN7u....'); // tss = target spreadsheet
  var ts = tss.getSheetByName('RAWData'); // ts = target sheet
  //set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);

}  

我从user2741获取了这个,发布了here

..这个适用于一张纸。 但现在我不确定如何从更多工作表中获取数据并实际对其进行排序。有没有人对如何做到这一点有好主意?

谢谢大家, 的Sascha

如果所有内容都发生在同一个电子表格中,则此处可以使用

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
mergeSheet2("SummarySheet", "123");
mergeSheet2("SummarySheet", "345");
mergeSheet2("SummarySheet", "748");
mergeSheet2("SummarySheet", "293");

function mergeSheet2(targetSheetName, sourceSheetName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName(sourceSheetName);
  var lastRow = sourceSheet.getLastRow();
  var lastCol = sourceSheet.getLastColumn();
  var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
  var destSheet = ss.getSheetByName(targetSheetName);
  var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
  source.copyTo(destRange, {contentsOnly: true});

}

但由于某种原因,我在使用不同的电子表格时无法运行它 错误:“目标范围和来源范围必须位于同一电子表格中”

我尝试过:

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
SpreadsheetApp.openById('ID');
mergeSheet2("SummarySheet", "123");


function mergeSheet2(targetSheetName, sourceSheetName) {
  var ss = SpreadsheetApp.openById('ID');
  var ssd = SpreadsheetApp.openById('ID');
  var sourceSheet = ss.getSheetByName(sourceSheetName);
  var lastRow = sourceSheet.getLastRow();
  var lastCol = sourceSheet.getLastColumn();
  var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
  var destSheet = ssd.getSheetByName(targetSheetName);
  var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
  source.copyTo(destRange, {contentsOnly: true});

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

解决方案比您尝试的更简单,因为您只想复制值并且知道copyTo仅适用于同一个电子表格,只需从源获取值并将其粘贴到目标中,如下所示:

function copyFromSourceToTarget(targetSheetName, sourceSheetName) {
  var ss = SpreadsheetApp.openById('ID');
  var ssd = SpreadsheetApp.openById('ID');
  var sourceSheet = ss.getSheetByName(sourceSheetName);
  var sourceData = sourceSheet.getDataRange().getValues();
  var destSheet = ssd.getSheetByName(targetSheetName);
  destSheet.getRange(destSheet.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}

为了使其更灵活,我建议使用4个参数并再次简化代码:

function copyFromSourceToTarget(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
  var ss = SpreadsheetApp.openById(sourceSsId)getSheetByName(sourceSheetName);
  var ssd = SpreadsheetApp.openById(targetSsId).getSheetByName(targetSheetName);
  var sourceData = ss.getDataRange().getValues();
  ssd.getRange(ssd.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}