Multiple sheet copying from a working sheet to an archive sheet

爷,独闯天下 提交于 2019-12-12 01:48:32

问题


I have had some help with the last line of this code and it works perfectly for a single sheet in the spreadsheet, however I have 5 sheets in the From spreadsheet and identical 5 sheets in the target spreadsheet how can I get the same range in the other 4 sheets to their same named counterpart in the target sheet? So I used below to try and copy each sheet to its corresponding namesake but I have set up the code as below but only importing the first sheet, also is there a way to only need to enter the originating spreadsheet id once for the whole code instead of at each sheet section?

 function ImportDataRange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
 sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('PM trip'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('PM trip'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
 sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('Pool / Beach'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('Pool / Beach'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
 sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('Night Dive'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('Night Dive'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
 sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
}

Thank you for any help. Much appreciated.


回答1:


The code can be simplified using a loop.

You said above that you have 5 sheets but you only have 4 in the code sample, add the other sheet name to var sheetNames.

function onOpen() { // This function adds a custom menu to the spreadsheet (Backup to archive) so you can run the script from there.

var ui = SpreadsheetApp.getUi();

    ui.createMenu('Backup to archive')
    .addItem('Backup', 'dataBackup')
    .addToUi();
}

function dataBackup() {

    var inputSS = SpreadsheetApp.openById('1wglM4-5jx873vwtFRPVgC1qk27JjrsYjDwp0fNpl5Xg'); // The file ID of the Input sheet
    var archiveSS = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g'); // The file ID of the archive sheet

    // The names of the sheets to be copied.
    // NOTE: These names must match the names of the sheets in BOTH spreadsheets i.e. the same case the spelling and the same spaces.
    var sheetNames = ['AM trip', 'PM trip', 'Pool / Beach', 'Night Dive'];

    for (var i = 0; i < sheetNames.length; i++) { // Loop to each sheet listed in 'var sheetNames' and copy the data.

        var inputSheet = inputSS.getSheetByName(sheetNames[i]);
        var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);

        var data = inputSheet.getRange('B7:U38').getValues();

        archiveSheet.getRange(archiveSheet.getLastRow() + 1, 2, data.length, data[0].length).setValues(data);
    }
}


来源:https://stackoverflow.com/questions/43598665/multiple-sheet-copying-from-a-working-sheet-to-an-archive-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!