I know that I'm asking a simple question, but surprisingly, I haven't been able to find where my exact question has been asked before.
Here's the situation: (This is method 1) I've got a sheet within a spreadsheet and I'd like to:
- copy/move the sheet to a new spreadsheet file in a specific (destination) folder, and
- rename the new spreadsheet to a value (string) located in a cell in the sheet from which the copied sheet was made
I couldn't figure out how to do both of the above at the same time, so next I tried to "split" the tasks (This is Method 2) as follows:
- Create a new spreadsheet file in my "Archive" folder
- Name the new spreadsheet file with a string value from the original report sheet (title desired is in cell C75 of original report sheet in original "Reports" folder)
- In the "Reports" folder, copy the original report sheet (containing values and formulas) to the new sheet (in the "Archive" folder) as values only
It doesn't matter how this happens - either Method 1 or Method 2. Nor does it matter to me if a method is used that I haven't considered yet.
After my (incomplete) script runs, I have one spreadsheet file containing two sheets - one sheet ("Report") composed of values and formulas, and another sheet ("Sheet") containing the exact same data as my "Report" sheet, but as values only.
I have blatantly copied parts and pieces of Google Script code from various webpages (including SO) to create the "values only" copy of my original spreadsheet and email it. All of this code works great. Here is what I have as a Google script so far:
function emailReportsAsPDF() {
// ***********************************************************************************************************************************
// This section creates a new blank sheet in the original spreadsheet file
// Get active spreadsheet/source file
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get the source sheet tab
var sourceSheet = ss.getSheetByName("Reports");
// Create a new sheet
var newSheetTab1 = ss.insertSheet("Sheet");
// Hide all gridlines in the new sheet
newSheetTab1.setHiddenGridlines(true);
// *************************************************************************************************************************************
// This section copies the original sheet as values (and formula results) to the new sheet that was just created
// Get desired data in the currently active sheet
var sourceDataRange1 = sourceSheet.getRange("A1:T164");
// Copy and paste the desired range to copy from the active sheet to the new sheet
sourceDataRange1.copyTo(newSheetTab1.getRange(1,1),{contentsOnly:true}); // Pastes values only
sourceDataRange1.copyTo(newSheetTab1.getRange(1,1),{formatOnly:true}); // Pastes cell formats only
sourceDataRange1.copyTo(newSheetTab1.getRange(1,1),SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS,false); // Pastes column widths only
// Hide the source sheet
// Note: Hiding the source sheet automatically makes the new sheet ('Sheet') active
sourceSheet.hideSheet();
// *************************************************************************************************************************************
// This section prepares variable info for an email
// Sets the recipient of the email message
var emailAddress = sourceSheet.getRange("M100:M100").getValues();
// Sets the subject of the email message
var subject = sourceSheet.getRange("C75:C75").getValues();
// Sets the 'body message' of the email message
var bodyMessage = "Please See Attached";
// **************************************************************************************************************************************
// This section copies the new sheet over to pdf and attaches it to the email that was just created
// Make the pdf
var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
// Set the title of the pdf
blob.setName("ReportSheet" + ".pdf");
// **************************************************************************************************************************************
// This section constructs and sends the email
GmailApp.sendEmail(emailAddress,subject,bodyMessage, {
htmlBody: bodyMessage,
attachments:[blob]
});
// ***************************************************************************************************************************************
//This section returns the original file to the way it was at the start of this script
// Show hidden source sheet ('Reports')
sourceSheet.showSheet();
// Delete new sheet ('Sheet')
var newSheetTab1 = ss.deleteActiveSheet();
// var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sourceSheet.getRange('A1');
range.activate();
// ****************************************************************************************************************************************
// This section copies the new sheet to a new spreadsheet in the 'Archived' folder
}
I have included all of this info to ask how I can do the last comment in the script, namely "This section copies the new sheet to a new spreadsheet in the 'Archived' folder"
Any help or guidance will be greatly appreciated.