简体   繁体   English

将数据从一个 Google 工作表工作簿工作表复制到另一个工作簿

[英]Copy Data from One Google sheets Workbook Sheet to another Workbook

So very much piecing together and learning google scripts and 1st challenge is to do the following: Several subsheets completed by agents who will use a script to collate onto a master sheet any that are not already on there.所以非常多地拼凑和学习谷歌脚本,第一个挑战是执行以下操作:由代理完成的几个子表,他们将使用脚本将任何尚未存在的主表整理到主表上。

Script will need to: Check if data in the subsheet table has been been already copied to master sheet (look at a cell for a 0 or 1).脚本需要: 检查子表中的数据是否已复制到主表(查看单元格中的 0 或 1)。 If 0 copy that to the last row of the master sheet.如果为 0,则将其复制到主工作表的最后一行。 I've used a countifs formula to populate the 0 or 1 on each sub sheet.我使用 countifs 公式填充每个子表上的 0 或 1。

I've managed to build the script to do this from one sheet to another in the same workbook but get an error message when trying to do so in different workbooks.我已设法构建脚本以在同一工作簿中从一张工作表到另一张工作表执行此操作,但在尝试在不同工作簿中执行此操作时收到错误消息。 Also found a clone script that does copy the data (whole page) to the master but overwrites and not last row and also ignores the 0 and 1 rule.还发现了一个克隆脚本,它确实将数据(整个页面)复制到主服务器,但覆盖而不是最后一行,并且也忽略了 0 和 1 规则。

Looked across many similar questions and online and struggling to see anyone with this same need and fix?浏览了许多类似的问题和在线并努力寻找有同样需求和解决方案的人?

Codes: Copy all the new data to another sheet in same workbook from the last row: /**代码:将所有新数据从最后一行复制到同一工作簿中的另一张工作表:/**

  • Retrieves all the rows in the active spreadsheet that contain Yes检索活动电子表格中包含 Yes 的所有行

  • in the Include column and copies them to the Report sheet.在“包括”列中并将它们复制到“报告”表中。 */ */

    function copyRows2() { function copyRows2() {

    var sSheet = SpreadsheetApp.getActiveSpreadsheet(); var sSheet = SpreadsheetApp.getActiveSpreadsheet();

    var srcSheet = sSheet.getSheetByName("Import2"); var srcSheet = sSheet.getSheetByName("Import2");

    var tarSheet = sSheet.getSheetByName("Report"); var tarSheet = sSheet.getSheetByName("报告");

    var lastRow = srcSheet.getLastRow(); var lastRow = srcSheet.getLastRow();

    for (var i = 2; i <= lastRow; i++) { var cell = srcSheet.getRange("C" + i); for (var i = 2; i <= lastRow; i++) { var cell = srcSheet.getRange("C" + i); var val = cell.getValue(); var val = cell.getValue(); if (val == "0") {如果(val ==“0”){

    var srcRange = srcSheet.getRange("A" + i + ":D" + i); var srcRange = srcSheet.getRange("A" + i + ":D" + i);

    var tarRow = tarSheet.getLastRow(); var tarRow = tarSheet.getLastRow(); var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":D" + (tarRow+1)); var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":D" + (tarRow+1));

    srcRange.setValues(tarRange); srcRange.setValues(tarRange); } } };` } } };`

Clone whole Sheet from workbook A to workbook B:将整个工作表从工作簿 A 克隆到工作簿 B:

function cloneGoogleSheet(ssA, ssB) {



 // source doc
  var sss = SpreadsheetApp.openById('Source Workbook ID');



// source sheet
  var ss = sss.getSheetByName('Import');

  // 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();

  // target spreadsheet
  var tss = SpreadsheetApp.openById('Target WorkbookID');

  // target sheet
  var ts = tss.getSheetByName('Master2');

  
  // set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);

};

Update: have a working code now of:更新:现在有一个工作代码:

/**
 * Retrieves all the rows in the active spreadsheet that contain Yes
 * in the Include column and copies them to the Report sheet.
 */

function MasterTracker() {
  var sSheet = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = sSheet.getSheetByName("DARS");
  var tSheet = SpreadsheetApp.openById('DOC ID');
  var tarSheet = tSheet.getSheetByName("TrackerPilot");
  var lastRow = srcSheet.getLastRow();
  
  for (var i = 2; i <= lastRow; i++) {
    var cell = srcSheet.getRange("A" + i);
    var val = cell.getValue();
    if (val == "0") {
      
      var srcRange = srcSheet.getRange("E" + i + ":U" + i);
      
      var tarRow = tarSheet.getLastRow();
            var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":Q" + (tarRow+1));
      
      var sourceValues = srcRange.getValues();
      tarRange.setValues(sourceValues);
    }
  }
};

But a new problem.而是一个新的问题。 On the source sheet there are cells with dropdowns from data validation - even when blank these delay the code when running.在源表上,有一些带有数据验证下拉列表的单元格——即使是空白的,这些单元格也会在运行时延迟代码。 Is there an easy way I am missing from Ignoring these rows if the lookup Cell is blank (not a 0 or 1)?如果查找单元格为空白(不是 0 或 1),是否有一种简单的方法可以忽略这些行?

function cloneGoogleSheet(ssA, ssB) {

 // source doc
  var sss = SpreadsheetApp.openById('Source Workbook ID');

// source sheet
  var ss = sss.getSheetByName('Import');

  // Get full range of data
  var SRange = ss.getDataRange();

  // get the data values in range
  var SData = SRange.getValues();

  var i = 0;
  var TData = [];
  for( i=0; i<SData.length; i++ ) {
    if( SData[i][2] === 0 ) { // Column C = 0
      TData.push(SData[i]);
    }
  }

  // target spreadsheet
  var tss = SpreadsheetApp.openById('Target WorkbookID');

  // target sheet
  var ts = tss.getSheetByName('Master2');

  
  // set the target range to the values of the source data
  ts.getRange(1,1,TData.length,TData[0].length).setValues(TData);  // Over write
  // Append ts.getRange(ts.getLastRow()+1,1,TData.length,TData[0].length).setValues(TData);

};

I haven't been able to test it but I'm pretty sure it will work.我无法对其进行测试,但我很确定它会起作用。

/**
 * Retrieves all the rows in the active spreadsheet that contain Yes
 * in the Include column and copies them to the Report sheet.
 */

function MasterTracker() {
  var sSheet = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = sSheet.getSheetByName("DARS");
  var tSheet = SpreadsheetApp.openById('DOC ID');
  var tarSheet = tSheet.getSheetByName("TrackerPilot");
  var srcData = srcSheet.getDataRange().getValues();  // get all values
  var sourceValues = [];

  for (var i=1; i<=srcSheet.getLastRow(); i++) {
    // i+1 is row number i is index in data array
    // A is column 1 so index is 0 in data array 
    if ( srcData[i][0] === 0 ) {  // assuming the value is a number?
      sourceValues.push(srcData[i].slice(4,21)); // Column E is index 4 and V (Not included) is index 21
    }
  }
  tarSheet.getRange(tarSheet.getLastRow()+1,1,sourceValues.length,sourceValues[0].length).setValues(sourceValues);  
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个工作表中复制有效范围并将数据粘贴到Google工作表中同一工作簿中不同工作表中的特定范围内? - How to copy active range from one sheet and pasting the data to a specific range in a different sheet within the same workbook in google sheets? 将数据从一个Google工作表工作簿移动到另一个工作簿 - Move Data From one google sheet workbook to another Google脚本 - 将数据从一个工作簿复制到另一个工作簿 - importrange()替代方案 - Google Script - Copy Data from one Workbook To Another - importrange() Alternative 将谷歌表格数据复制到另一个工作簿,文本公式为 static 文本 - Copy google sheets data to another workbook with text formula as static text 将工作表中的数据复制到另一个工作簿的最后一行 - Copy data from sheet to the last row of another workbook 将一个工作簿中的值粘贴到另一个工作簿底部的脚本-Google表格 - Script to paste values from one workbook into the bottom of another - google sheets 将数据从一个工作簿提交到另一个工作簿 - submit data from one workbook to another workbook 将工作簿 A 的工作表 1 复制到工作簿 B 的工作表 1 - Copy Sheet 1 from Workbook A to Sheet 1 of Workbook B 如何使用 zapier 自动将填充有 zapier 的新行的值复制到具有 google 工作表的同一工作簿中的另一个工作表? - How to automaticaly copy values of a new row populated with zapier to another sheet in same workbook with google sheets? 在 Google 表格中,如果起始工作表的尺寸是动态的,如何使用脚本将选项卡复制到 SAME 工作簿中的另一个选项卡? - In Google Sheets, how to copy tab to another tab in SAME workbook using Script, if the dimensions of the starting sheet are dynamic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM