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
Related
I have an apps script that creates a new Google workbook each time a new entry is made in Column A of sheet1 of a Master workbook and renames that new workbook to the last entered data in column A of sheet1 of the Master workbook. It also pastes the URL of the newly created workbooks in Column J of sheet1 of the master workbook.
The apps script code is run by an onEdit trigger I set manually. Each time a new workbook is created, the last row that was edited gets copied (from A to J) to the newly created workbook. Every workbook both the master workbook and the newly created workbooks gets shared with a list of emails typed in Column B of Sheet 2 of the master workbook (as editors). Below is the code.
function myFunction() {
// Creating new Spreadsheet.
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const lastRow = sh.getLastRow();
const values = sh.getRange(`A${lastRow}:J${lastRow}`).getValues()[0];
const nss = SpreadsheetApp.create(values[0]);
const url = nss.getUrl();
sh.getRange(lastRow, 10).setValue(url);
values[9] = url;
nss.appendRow(values);
// Sharing Spreadsheets.
const sheet2 = ss.getSheetByName("Sheet2");
const emails = [...new Set(sheet2.getRange("B1:B" + sheet2.getLastRow()).getValues().reduce((ar, [b]) => {
if (b && b.includes("#")) ar.push(b);
return ar;
}, []))];
ss.getEditors().forEach(e => ss.removeEditor(e));
ss.addEditors(emails);
nss.addEditors(emails);
}
I have been trying to make the master workbook copy a template sheet (by ID), but I haven't done so successfully. I need your help on this. Your help is appreciated.
Copy sheet from Master to new Spreadsheet:
const MasterSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const NewSpreadsheetId = SpreadsheetApp.create(`New`).getId()
const NewSpreadsheet = SpreadsheetApp.openById(NewSpreadsheetId)
MasterSpreadsheet.getSheetByName(`TargetSheet`)
.copyTo(NewSpreadsheet)
.setName(`CopiedSheet`)
Copy Spreadsheet to new Spreadsheet:
const MasterSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const NewSpreadsheetId = MasterSpreadsheet.copy(`Copy of ${MasterSpreadsheet.getName()}`).getId()
const NewSpreadsheet = SpreadsheetApp.openById(NewSpreadsheetId)
Note: Copying a Spreadsheet will place the new Spreadsheet file in your Drive 'root' folder. You can however, use DriveApp to get the file using NewSpreadsheetId and move it to a specified folder.
If neither of these help, or you need further explanation, please let me know!
I am currently creating a script file bounded to a spreadsheet, where this spreadsheet is stored in a folder. Now, I need the name of the folder in which this spreadsheet exists. So how do I achieve this?
Solution:
The following script will get the name of the parent folder of the bound spreadsheet and set it to cell A1. Adjust the script to your needs.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const ss_id = ss.getId();
const file = DriveApp.getFileById(ss_id);
const parent_folders = file.getParents()
const folder_name = parent_folders.next().getName(); //desired result
sh.getRange('A1').setValue(folder_name);
}
folder_name is the name of the parent folder of the existing spreadsheet.
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);
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)
I am trying to create a Google Sheet template where a user can enter a value (Project URN) in a cell and run a script to save the template as a sheet, renamed with the Project URN.
I have created a test Google Sheet here https://docs.google.com/spreadsheets/d/1IPpQyYHl_TtNa1lGpmu4dlzwTFjOotV2OcnxFT84iUk/edit?usp=sharing
This sheet has the following script, but I cant get the renaming function to correctly work.
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B7RrbZuJGLlha3NvM3ZqcTY3MDA");
DriveApp.getFileById(sheet.getId())
.(makeCopy(values = SpreadsheetApp.getActiveSheet()
.getRange(2, 2).getValues()), destFolder);
} //END function saveAsSpreadsheet
I did also find the following article but wasn't able to turn it to my use. However, the secret must be here somewhere! Rename worksheet to cell value keeping format
Kitten
You almost have it. Please try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
var id = ss.getId(); // Get spreadsheet ID
var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet to be created
var sheet_name = sheet.getRange("A1").getValue(); // Get the value, in this case: Project Urn
var folder = DriveApp.getFolderById("XXXX"); // Get the folder where the sheet needs to be placed.
sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.
}
Let me know if you have any questions.