I’m working with a Google Spreadsheet that contains three sheets:
- Sheet A has all my combined data
- Sheets B and C contain report formats with filtered data.
I need to download only Sheets B and C together, but I can only find a way to download individual sheets using their GID. Is there an efficient way to download both Sheets B and C at once without including Sheet A?
Olivia Maria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0
Download specific sheets from a spreadsheet
You can achieve “downloading” a new spreadsheet with a unique name containing the two specific sheets that you want by following these steps:
This will involve creating a google apps script that is connected to your original spreadsheet(which contains the three sheets)
This script will create a new spreadsheet then will copy sheet B and Sheet C to that new spreadsheet and this new spreadsheet will then be saved to your Google Drive, which you can download.
Set-up script
Open your spreadsheet that contains the three sheets then at the navigation menu at the top, click “extensions” then click apps script
Google apps script editor will open on a new browser window, this is where you will paste the code below(clear the contents of the script before pasting my code)
On the apps script editor, click the run button located at the menus at the top
After that, an execution log will appear at the bottom of the screen where you will find the link of the newly created spreadsheet, copy that link and paste it on a new browser window to access the new spreadsheet
You will now access the new spreadsheet that contains the two sheets
You can also check the new spreadsheet named (“Exported Sheets”+ current date)
Sample Code
function exportSheets() {
// Open the current spreadsheet(where the three sheets are present)
var ss = SpreadsheetApp.getActiveSpreadsheet();
//for pasting the link of the new spreadsheet
var sheetA = ss.getSheetByName("Sheet A");
// Create a new spreadsheet where sheet B and C will be stored together
var date = new Date();
var newSpreadsheet = SpreadsheetApp.create('Exported Sheets '+date);
// Get the two sheets, sheet B and C
var sheetB = ss.getSheetByName('Sheet B'); //replace with the appropriate name of the 'sheet B' from the spreadsheet
var sheetC = ss.getSheetByName('Sheet C'); //replace with the appropriate name of the 'sheet C' from the spreadsheet
// Copy sheets B and C to the new spreadsheet
if (sheetB) {
sheetB.copyTo(newSpreadsheet).setName('Sheet B');
}
if (sheetC) {
sheetC.copyTo(newSpreadsheet).setName('Sheet C');
}
// Check if there is a default empty sheet on the newly created sheet and remove it if present
var sheets = newSpreadsheet.getSheets();
if (sheets.length > 1) {
var defaultSheet = sheets[0];
if (defaultSheet.getName() === 'Sheet1') {
newSpreadsheet.deleteSheet(defaultSheet);
}
}
// Log the URL of the new spreadsheet
Logger.log('New Spreadsheet URL: ' + newSpreadsheet.getUrl());
//This line will paste the link on cell G10 of your sheet A on the original sheet(change the cell if needed)
sheetA.getRange("G10").setValue(newSpreadsheet.getUrl())
}
This updated code will paste the link of the new sheet on cell G10 of sheet A(see image below) located on your original sheet assuming that your button which you assigned the script to is on Sheet A, do not hesitate to say so if that is not the case.
References: Apps script’s SpreadSheetApp, SpreadsheetApp copyTo()
3
If you don’t need to automatise the process for a large number of spreadsheets, you could simply manually create a new temporary spreadsheet with a copy of the two sheets.
Click on the arrow of (or right-click on) sheet B, then ‘Copy to’ -> ‘New spreadsheet’. Then on sheet C: ‘Copy to’ -> ‘Existing spreadsheet’, and select the previously created spreadsheet containing the copy of sheet B.
Then just download the spreadsheet.
user27425627 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.