Copying google spreadsheet columns from one spreadsheet to another with a script

后端 未结 3 1775
日久生厌
日久生厌 2021-02-10 14:17

I\'d like to be able to copy a colum (well 3) from a spreadsheet to another spreadsheet (google docs spreadsheets). I\'d also like there to be some sort of trigger that looks fo

3条回答
  •  野性不改
    2021-02-10 14:39

    I have found that it is easier to just copy the source_sheet to the target_spreadsheet, copy the necessary range using copyTo then delete the copied source_sheet.

     // 1. Open source sheet
     let spreadsheetFrom = SpreadsheetApp.openById(source_spreadsheet_id);
     let sheetFrom = spreadsheetFrom.getSheetByName(source_sheet_name);
    
     // 2. Copy source sheet to target spreadsheet
     let spreadsheetTo = SpreadsheetApp.openById(target_spreadsheet_id);
     let sheetCopy = sheetFrom.copyTo(spreadsheetTo);
    
     // 3. Copy range from source_sheet's copy to target_sheet
     let rangeFrom = sheetCopy.getRange('XX:YY');    
     let sheetTo = spreadsheetTo.getSheetByName(target_sheet_name);
     let rangeTo = sheet.getRange('XX:YY');
     rangeFrom.copyTo(rangeTo);
    
     // 4. Delete source_sheet's copy
     spreadsheetTo.deleteSheet(sheetCopy);
    

提交回复
热议问题