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.
Related
I have written a script that duplicates an entire google sheet to a new document and saves it in a folder when a button is pressed, however, I only want it to copy a specific range (A1:F52).
Every attempt I make at modifying the script to only have .getRange("A1:F52") throws an error that getrange is not a function, what am I doing wrong and how should it actually be?
This script functions perfectly as it is, but I just want it to only take the range across, any help would be greatly appreciated.
// SAVE INVOICE
function copyRowsWithCopyTo() {
// SET VALUES IN CELL
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1:F60').activate();
spreadsheet.setCurrentCell(spreadsheet.getRange('F3'));
spreadsheet.getRange('A1:F60').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//COPY CONTENTS TO INVOICE SHEET
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let sourceSheet = spreadSheet.getSheetByName('DO NOT EDIT');
// SET SPREADSHEET MONTH
let targetSheet = spreadSheet.getSheetByName('JANUARY 2023');
let row = sourceSheet.getActiveRange().getRow();
let activeRow = sourceSheet.getRange( 2,1,1,20);
let last_row = targetSheet.getLastRow();
activeRow.copyTo(targetSheet.getRange('A'+(last_row+1)+':H'+(last_row+1)),{contentsOnly:true});
// REPLICATE INVOICE SHEET TO NEW DOCUMENT AND SAVE IN INVOICE FOLDER
var sheet = SpreadsheetApp.getActiveSheet(); // Get current active sheet.
//GET SHEET NAME
var sheet_name1 = sheet.getRange("F4").getValue(); // Get the value of cell F4, used to name the new spreadsheet.
var sheet_name2 = " - "
var sheet_name3 = sheet.getRange("A9").getValue(); // Get the value of cell A9, used to name the new spreadsheet.
var sheet_name = sheet_name1 + sheet_name2 + sheet_name3
var folder = DriveApp.getFolderById("1DVdganPrCfePk41kteKnXSqKmL4Pivu5");
// Get the ID of the folder where you will place a copy of the spreadsheet.
var newSS = SpreadsheetApp.create(sheet_name); // create new blank spreadsheet in a root folder
var asFile = DriveApp.getFileById(newSS.getId()); // get new spreadsheet as a file
folder.addFile(asFile); // add this file to destination folder
DriveApp.getRootFolder().removeFile(asFile); // remove a file from root folder
var copiedSheet = sheet.copyTo(newSS).getRange("A1:F52"); // copy active sheet to new spreadsheet
copiedSheet.setName(sheet_name); // rename copied sheet
newSS.deleteSheet(newSS.getSheetByName('Sheet1')); // remove "Sheet1" sheet which was created by default in new spreadsheet
// run Clear function
clear()
}
I am clearly mistyping the .getrange parameter as it throws an error, but I cannot work out what im doing wrong.
I've been reading this without making any headway:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
Is there a way to programmatically get the Spreadsheet ID and Sheet ID of a particular Google SpreadSheet's sheet without manually typing or copying the URL?
1.) If the script is bound to the spreadsheet then you can just use getID and getSheetID as mentioned by TheMaster in the comments. See codes below:
function getIDs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var spreadSheetID = ss.getId();
var sheetID = sheet.getSheetId();
console.log(spreadSheetID);
console.log(sheetID);
}
Result:
Take note that the first sheet always has an ID of 0 (gid=0) as the default value.
2.) Otherwise if what you are trying to do is get the Spreadsheet ID and Sheet ID from different spreadsheets it is not possible without copying the URL since there is no way it can identify which spreadsheet to refer to.
3.) Suggestion
What you can do if you need to get IDs of different files is to move them to a folder in your drive, then you can use the code below:
function listFilesInFolder(folderName) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
sheet.appendRow(["Name", "Spreadsheet ID", "Sheet ID"]);
//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
var folder = DriveApp.getFolderById("HIS_SHOULD_BE_YOUR_FOLDER_ID");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
data = [
file.getName(),
file.getId(),
];
var spreadsheetSheets = SpreadsheetApp.openById(data[1]).getSheetByName("Sheet2").getSheetId();
data.push(spreadsheetSheets);
sheet.appendRow(data);
};
};
Folder:
Result:
Note: In my example code I've made use of Sheet2 to see that it is working as expected. Please set accordingly to the name of the sheet which you are trying to get the ID.
Let me know if this works for you or if you have other questions.
References:
Are Google Spreadsheets' first sheets always given an ID of 0?
Getting all files ID in drive folder
I'm in a situation where I need to send data to specific tabs(sheet) in a google sheet file (basically it's a G.Sheet file with one sheet that is a "template").
two cases: a/ the needed sheet exists then just append the data in that one, if not then use a specific sheet (template) copy it to create the new sheet as a new tab and append in it the data sent.
Each new sheet get its name as a parameter in the flow of data to be appended.
I'm really confused and know where to start and hope I'm clear enough.
So far I have this little code (not including the copying of the template cuz I don't know how to do it).
// pseudo is the name of the sheet in the file
function doGet(e) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/IDXXX/edit#gid=0");
var sheet = ss.getSheetByName(e.pseudo);
addData(e,sheet);
}
function doPost(e) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/IDXXX/edit#gid=0");
var sheet = ss.getSheetByName(e.pseudo);
addData(e,sheet);
}
function addData(e,sheet) {
//some data to append in the e.pseudo sheet
var id = e.parameter.id ;
var date = e.parameter.date ;
var textRef = e.parameter.textRef ;
var niveau = e.parameter.niveau ;
var cycle = e.parameter.cycle ;
var vitesse = e.parameter.vitesse ;
var reference = e.parameter.reference ;
sheet.appendRow([id,date,textRef,niveau,cycle,vitesse,reference]);
}
I would have thought e.psuedo should have been e.parameter.psuedo
You can refer to this sample code on how to create a new sheet from a template sheet. Assuming your sheet name can be obtained in e.pseudo or in e.parameter.psuedo as what #Cooper advised.
// pseudo is the name of the sheet in the file
function doGet(e) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/IDXXX/edit#gid=0");
var sheet = ss.getSheetByName(e.pseudo);
// Sheet doesn't exit, create new sheet from a template
if (sheet == null) {
// Select "template" sheet and copy to the current Spreadsheet
sheet = ss.getSheetByName("template").copyTo(ss);
// Rename the copied template sheet
sheet.setName(e.pseudo);
}
addData(e,sheet);
}
What it does?
getSheetByName(name) will return null if the sheet name doesn't exist. Select your template sheet and use copyTo(spreadsheet) to create a copy in your destination spreadsheet.
rename the newly copied sheet based on your input sheet name using setName(name)
Note:
just replace the name of your template sheet
trying to figure out how to give a new sheet that is copied into a folder a specific name each time from a button script. The specific name would come from a specific cell each time.
Here's the current code being used and the New Sheet is what I'm trying to have named from cell B3 on a specific spreadsheet each time. No idea how to do this, it just names it NEW SHEET each time and then errors out when I try to do B3.
function cloneGoogleSheet() {
var destFolder = DriveApp.getFolderById("Folder Name");
DriveApp.getFileById("Folder Name").makeCopy('NEW SHEET', destFolder);
SpreadsheetApp.getActive().getSheetByName('NEW SHEET').setName('Specific Sheet!B3')
}
Any help is appreciated!
function cloneGoogleSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const name = sh.getRange('A1').getDisplayValue();
const destFolder = DriveApp.getFolderById("Folder Id");
DriveApp.getFileById("file id").makeCopy(name, destFolder);
}
Get used to using file id's and not names.
I am creating a copy of my google spreadsheet, and now want to close the current spreadsheet and open the one i created a copy. I know i can open it using the sheet id, but i dont know the sheet id yet because I want to achieve this using one macro!
current code -
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
//want to open this copied file
}
By modified your code a little bit, then you can obtain the copy sheet Id with newsheet.getId() function:
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxV6N-A4Z77f9OijHtAe2VhAU");
var newsheet = DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
Logger.log(newsheet.getId())
}