简体   繁体   中英

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). If 0 copy that to the last row of the master sheet. I've used a countifs formula to populate the 0 or 1 on each sub sheet.

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.

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

  • in the Include column and copies them to the Report sheet. */

    function copyRows2() {

    var sSheet = SpreadsheetApp.getActiveSpreadsheet();

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

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

    var lastRow = srcSheet.getLastRow();

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

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

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

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

Clone whole Sheet from workbook A to workbook 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)?

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);  
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM