How to change Google Sheets file name based on cell information - google-apps-script

I am trying to rename my Google Sheets File name based on contents of at least one cell (ideally, it would be a combination of information from two cells: B2 and D2, but I can make contents of one cell work).
I am able to change the worksheet name from the contents of one cell, but am struggling to change the actual file name.

To change the Spreadsheet's name you will need DriveApp too and change its filename. Try this example:
// get the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get a value from the B2 cell on sheet "Sheet" as new filename
var newFileName = ss.getRange("Sheet!B2").getValue();
// get the spreadsheet ID
var ssID = ss.getId();
// get the spreadsheet file by ID
var ssFile = DriveApp.getFileById(ssID);
// set new filename for the spreadsheet
ssFile.setName(newFileName);

Related

Google script to email link to sheet saved as a result of a previous script

I have a Google Form and it's connected to a Google Sheet.
Once the new data hits the sheet, I have a trigger set to save the file as a new file named by a specific cell (TestRange - below).
I want to send an email to the most recent email entry. The email should contain the URL to the most recently saved file.
The idea is that the user who entered the form data gets their specific copy of the sheet that I saved for them.
Here is my code:
function myFunction(){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PQReport").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange('PQData!C3');
ss.setNamedRange('FileName', range);
var TestRange = ss.getRangeByName('FileName').getValue();
Logger.log(TestRange);
var destFolder = DriveApp.getFolderById("aswwed...askdjksjdffkjdkf");
DriveApp.getFileById(ss.getId()).makeCopy(TestRange, destFolder); '
}
The file is saved as the cell value of PQData!C3 in the destFolder.
The email address I want to use is the last row of PQData!CV (column 48). If there was a way to save the URL of the recently saved file to column 50, then call to that column to pick up the URL in the email, that would be ideal.
Thanks in advance for any help!
Issue:
Assuming that you want to do the following:
Retrieve the URL from the copied file made in DriveApp.getFileById(ss.getId()).makeCopy(TestRange, destFolder);.
Save this URL to the last row, column 50 of sheet PQData.
Retrieve an email address from last row, column 48 of sheet PQData.
Send an email to this email address, containing the URL of the file.
Solution:
Use File.getUrl() to get the URL of the file.
Use getLastRow() to get the last row in your sheet.
Use getRange(row, column) to retrieve a cell based on the row and column coordinates (no need to use A1 notation).
Use MailApp.sendEmail(recipient, subject, body) to send your email, with the URL on the body parameter.
function myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pqDataSheet = ss.getSheetByName("PQData");
var range = pqDataSheet.getRange('C3');
ss.setNamedRange('FileName', range);
var TestRange = ss.getRangeByName('FileName').getValue();
var destFolder = DriveApp.getFolderById("aswwed...askdjksjdffkjdkf");
var file = DriveApp.getFileById(ss.getId()).makeCopy(TestRange, destFolder);
var url = file.getUrl();
var lastRow = pqDataSheet.getLastRow();
pqDataSheet.getRange(lastRow, 50).setValue(url);
var email = pqDataSheet.getRange(lastRow, 48).getValue(); // Alternatively getRange("AV" + lastRow)
MailApp.sendEmail(email, "MY SUBJECT", url);
}
Notes:
PQData!CV does not refer to column 48, it would be AV instead. Please be careful with this.
I removed mention of PQReport since you are not using it in your code, please add it if necessary.
Why setting a named range and getting a value from that range instead of directly retrieving the value from C3?

Script to Save a copy of Google Sheets

I am trying to find a way of creating a script that will copy a GoogleSheet, which is the Master file, into a new location with a new name. The name of the file would be located in Cell A1 with the folder location in Cell A2. With Excel it was relatively easy, but I just can't seem to get anything to work with GoogleSheets.
Assuming this is the Master file:
Replace cell A2 with the folder id of the destination folder, choose the name of the copy in cell A1 and change Sheet1 to your specific case.
Copy and execute this function from the script editor:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
const new_name = sh.getRange('A1').getValue();
const dest_folder_id = sh.getRange('A2').getValue();
const dest_folder = DriveApp.getFolderById(dest_folder_id);
DriveApp.getFileById(ss.getId()).makeCopy(new_name, dest_folder);
}
References:
makeCopy(name, destination)
getRange()
Class Spreadsheet

Google app script to cut and paste a range from a google sheet to another google sheet

I'm trying to put together a script that cuts a range from a google spreadsheet and pastes it to another spreadsheet. The script works fine as long as the source sheet is the first sheet in my document. How can I fix it so that the script grabs the right sheet no matter where it is in my document?
I'm very very new at this and mostly used code I found online. Probably made a very obvious mistake..
function CopyRangeTo_OtherSheet() {
var sss = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var ss = sss.getSheetByName('Sheet1');
var range = ss.getRange(2,4,sss.getLastRow()-1,9);
var data = range.getValues();
var tss = SpreadsheetApp.openById('1WHoETM3QxV166ndnPW4Qv37Y6SBGPVw-CcEQxNpw0Co');
var ts = tss.getSheetByName('Sheet3');
ts.getRange(ts.getLastRow()+1, 1,50,9).setValues(data);
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
}
The number of rows will be different in each sheet. .getLastRow() on a spreadsheet class only returns the last row in first sheet. getLastRow of sheet instead
ss.getLastRow()
Your line var ss = sss.getSheetByName('Sheet1'); implies that you take the sheet with the name Sheet1.
This is the sheet you use to obtain your values from range and subsequently copy them.
If you want to copy / cut and paste values from a different sheet, simply paste the correct name of the sheet in var ss = sss.getSheetByName('PASTE HERE THE NAME OF THE SHEET');
Small annotation:
If the range you want to clear is the same from which you copied the
values, you do not need to define it again. You can simply replace the
lines
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
through
range.clearContents();
Also note:
setValues() only works if the range into which the values shall be set has the same dimension like the value range, i.e. the original range.
If your original range is 2,4,ss.getLastRow()-1,9, then the number of rows is ss.getLastRow()-1 and the number of columns 9.
Thus, you need to define
ts.getRange(ts.getLastRow()+1, 1,ss.getLastRow()-1,9).setValues(data);
to adapt the row number dynamically.

App script to loop though Google Sheets and cel range then gets a matching sheet to file id then copy data to id file

Narration:
I am a newbie to app script, I have a spreadsheet which has several sheets named after different client. Also, I have in the sheet named “Customer” a range of customer name and file ID of the original customer files on my drive
Objective:
I want to loop through the sheets (A,B,C,D ETC) and copy the values in each sheet and then loop through the range in the sheet named “customer” get the corresponding file ID for the particular customer.
Then to open the corresponding customer file, paste the data into the file and go on looping to the next sheet and get subsequent spreadsheet file of the next customer and so on.
All help is well appreciated.
My Example Spreadsheet is here [my sample spreadsheet][1]
My Code: am not sure how to proceed. (I really want to learn)
function updatefile() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('CUSTOMER')
var data = sh.getDataRange().getValues(); // get the range of non emmpty cells
var allsheets = ss.getSheets(); // get an array of all sheets and their name
// Array holding the names of the sheets to exclude from the execution
var exclude = ["ALL_CUSTOMER_ORDERS","CUSTOMER","TEMPLATE"];
for(var s in allsheets){
var sheet = allsheets[s];
// Stop iteration execution if the condition is meet.
if(exclude.indexOf(sheet.getName())==-1) continue;

Copy a range in active sheet, create new spreadsheet with custom name, paste range

I recently wrote my first Google Apps script that makes a copy of a spreadsheet (including all tabs within that spreadsheet) and places it into a specific folder in the user's Drive. The copy is renamed based on a cell value in the original spreadsheet. Here is my script, for reference:
function copyDocument() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get current active spreadsheet.
var id = ss.getId(); // Get current active spreadsheet ID.
var sstocopy = DriveApp.getFileById(id); // Get spreadsheet with DriveApp.
var sheet = ss.getActiveSheet(); // Get current active sheet.
var sheet_name = sheet.getRange("B1").getValue(); // Get the value of cell B1, used to name the new spreadsheet.
var folder_name = sheet.getRange("C23").getValue(); // Get the target folder ID.
var folder = DriveApp.getFolderById(folder_name); // Get the ID of the folder where you will place a copy of the spreadsheet.
sstocopy.makeCopy(sheet_name,folder); // Make a copy of the spreadsheet in the destination folder.
}
This script works, but I have been asked to modify it because the script I wrote is copying over unnecessary tabs and data that is causing confusion to users.
The new script should make a copy of a specific range in a specific sheet, create a new spreadsheet, and paste that range into it. It should also name itself after a cell value in the range.
However, the only method I have come across that specifically copies a sheet into a new spreadsheet is copyTo(spreadsheet). However, the Google Apps Script Guide specifies that "the copied sheet will be named 'Copy of [original name]'" by default.
I want to be able to rename the copied sheet after a specific cell. My question is, can I use copyTo(spreadsheet) and give the new spreadsheet a custom name, based on a cell?
Thanks!
You will need to get the data from the specific sheet to copy out to a variable of using:
var sourceSheet = ss.getSheetByName("Sheet1");
var sourceData = sourceSheet.getDataRange().getValues();
var originalRangeNotation = sourceSheet.getDataRange().getA1Notation();
Then you need to create a new, empty file and make a sheet with the same name
var ssNew = SpreadsheetApp.create("My New File Name");
ssNew.insertSheet('My New Sheet');
Then add the contents from the saved data to the new file. Since the insertSheet makes the new sheet the active one, we use:
var sheetNew = ssNew.getActiveSheet();
var rangeNew = sheet.getRange(originalRangeNotation);
range.setValues(sourceData);
Thanks. I ended up using this in place of sstocopy.makeCopy()
folder.addFile()
new_sheet.getActiveSheet().getRange("X:Y").setValues(sheet.getRange("X:Y").getValues())
DriveApp.getRootFolder().removeFile(temp)