Name a sheet based on a cell in that sheet - google-apps-script

Im looking to make 60+ copies of a template spreadsheet using the google sheets API. I would like to be able to name each sheet using a cell in that same sheet.
function NewBracket() {
var spreadsheet = SpreadsheetApp.getActive();
var name = spreadsheet.getActiveRange().activate() ;
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Template'), true);
spreadsheet.duplicateActiveSheet();
spreadsheet.getRange('B1').activate();
name.copyTo(spreadsheet.getActiveRange(),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getActiveSheet().setName(name);
};
When I run this, the sheet is created properly, the cell B1 is named properly, but the newly created sheet is named Range, not the name that is input into cell B1. How do I get the above code to name the sheet the same name that shows up in the B1 cell?

The answer came from user ra89fi in the comments, but it worked. I changed spreadsheet.getActiveSheet().setName(name); to spreadsheet.getActiveSheet().setName(name.getValue()); and it worked perfectly.

Related

Google Apps Script to copy range in spreadsheet using sheet reference (not sheet name)

I need to copy a range in a spreadsheet from one row to another - but the name of the sheet changes and I am unable to work out how to use the sheet reference rather than sheet name when referring to the range.
Example code below which works - however I need to tell GAS to go get the first sheet, not sheet "S1"
function copyRange() {
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheetByName("S1")
var source_range = sheet.getRange("A2:G2");
var target_range = sheet.getRange("A3:G3");
source_range.copyTo(target_range);
}
About Example code below which works - however I need to tell GAS to go get the first sheet, not sheet "S1", in this case, how about the following modification?
From:
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheetByName("S1")
To:
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheets()[0];
By this modification, the 1st tab is used as sheet. When [0] of getSheets()[0] is changed to [1], the 2nd sheets is used. When you changed the arangement of the sheet, the sheet is changed. For example, when you move the 1st tab from 3rd tab, getSheets()[0] retrieves the current 1st tab. Please be careful about this.
Reference:
getSheets()

Rename a Google sheet tab based on cell

I partially know the name of 2 sheets I need to rename when the cell B2 change in each sheet:
One in called csv_nat and it should be renamed to csv_nat_30_11_2019 when cell B2 in sheet changes (it is a date format like 30/11/2019)
The other one has the exact same structure and is called csv_alt
My main issue is the following. When the tab name has already changed, I need to locate the sheet from its root name (csv_nat) and I have not figured out how.
My limited and starting code for now is:
function onEdit(e){
if(e.range.getA1Notation() != "B2") return;
e.source.getActiveSheet().setName("csv_nat_"+e.value);
}
Besides, does the onEdit function work for any change or only when user edits the cell?
If you want to locate your csv_nat or csv_alt sheets, you can use regex in this case.
Try this example
if sheet contains csv_nat or csv_alt it will alert you:
var ss = SpreadsheetApp.getActiveSheet();
if(ss.getSheetName().match(/csv_nat|csv_alt/)){
SpreadsheetApp.getUi().alert("Working Sheet is found")
}
Reference
String.match()

Copy cell values from one spreadsheet to a second AFTER the second has been named after the first

We have two different spreadsheet templates; a Sales spreadsheet composed of several linked tabs
(Formulas work fine and we like the layout). We also have a Management Spreadsheet with several interlinked tabs(formulas work and we like the layout on this one too).
They are both templates that we copy, rename, and use for each new project while preserving the originals and associated formulas as "Blank Sales templates" and "Blank Management Templates" respectively.
Not all projects that we create proposals for actually get contracted so, often times we do not utilize the management Template at all. When we do, we have a script that allows us to copy, name, and place the Management Spreadsheet from a button inside the Sales spreadsheet. This works fine but only the Sales file name is copied to the Man File and we ' d like to copy over cell data as well.
When the "Copy Man. File " button is clicked from the Sales File, we want specified cell values from the Sales File Cover Sheet to copy over to different cells in the Man file cover sheet. Looks easy...Sounds easy....it Aint!
Both the Man. Spreadsheet and Sales Spreadsheets contain summary-like, "Cover sheet" tabs and although the data between them is mostly redundant, it is not formatted the same.
I Suspect that this cannot be done this way because we need to copy the data over as the new file is created. Can anyone at least confirm this is possible?
Here is the code so far:
function CreateManFile9() {
var copyMan = DriveApp.getFileById("Man. Template").getId();
var toFolder = DriveApp.getFolderById("Man. Template folder");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//button in one cell of active sheet runs this script
var sheetName = sheet.getRange('B3').getDisplayValue(); //
//Can't we grab additional cells, at this point, and copy them over too?
DriveApp.getFileById(copyMan).makeCopy(sheetName, toFolder);
}
function copyValuesSrcToDes(srcId,srcshname,srcrgA,desId,desshname,desrgA) {
var src=SpreadsheetApp.openById(srcId);
var srcsh=src.getSheetByName(srcshname);
var des=SpreadsheetApp.openById(desId);
var dessh=des.getSheetByName(desshname)
srcrgA.forEach(function(sA1,i){
dessh.getRange(desrgA[i]).setValue(srcsh.getRange(sA1).getValue());
});
}
srcId - Source Spreadsheet Id
srcshname = Source Sheet Name
srcrgA - An array of cells to copy from in A1 notation
desId - Destination Spreadsheet Id
desshname - Destination Sheet Name
desrgA = An array of cells to copy to in A1 notation
This is what I think you need to do:
function CreateManFile9() {
var copyMan = DriveApp.getFileById("Man. Template").getId();
var toFolder = DriveApp.getFolderById("Man. Template folder");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetName = sheet.getRange('B3').getDisplayValue(); //
var file2=DriveApp.getFileById(copyMan).makeCopy(sheetName, toFolder);
var srcId=copyMan;
var srcshname=sheet.getName();
var srcrgA=['B4','B5','B10','D11','E4'];
var desId=file2.getId();
var desshname=SpreadsheetApp.openById(desId).getSheets()[0].getName();
var desrgA=['B8','B10','D15','E3','J14']
copyValuesSrcToDes(srcId,srcshname,srcrgA,desId,desshname,desrgA)

Google Sheets script to copy/paste from one sheet to another

Could someone help me with creating a script that does the following:
Function: Upon adding a new sheet to my already existing workbook, I would like it to copy Column "E" from Sheet 1 and (Paste Special > Conditional formatting only) to the newly introduced Sheet "X"
Where can I learn more on how to write code? I have never used Stackoverflow by the way someone just recommended me to come here. I believe I just answered my own question somehow on the post, which I am sure was wrong to do but I couldn't comment on an already existing answer without exceeding the limit.
// Adds custom menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('CustomMenu').addItem('Copy format', 'copyFormat') //Add function to menu.'GIVE NAME HERE', 'functionName'
.addToUi();
}
function copyFormat() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheets()[0]; //Gets the first sheet of the workbook. Can use ss.getSheetByName('Name of sheet here'); If it is not the first sheet
var activeSh = ss.getActiveSheet(); //Get the active sheet, you should be on the sheet just added
var rowEnd = activeSh.getLastRow(); //Last row of active sheet
sh1.getRange("E1:E").copyFormatToRange(activeSh, 5, 5, 1, rowEnd); //Copy format, including conditional, to column E of active sheet
}
This just adds a button that allows you to select a cell and give it the same conditions has in the original sheet.
Example: Sheet 1: Column E1:E100 has a given condition.. which needs to apply in the exact same way to any new incoming sheets since they all come in the same format. Right now its at a point of which when a new sheet arrives in the workbook: I can enter the new sheet > Select the cell which requires the conditions > Select the custom menu > Cell gets conditioned. So the next step would be automate the process I just mentioned since there is several sheets added daily to the workbook.

copy cell values of named range in one sheet to same cell address of another named range in another sheet

Is it possible to copy the values of a named range in one sheet and paste them in the same cell location of another sheet?
For example, I have named range "old_desk_1" in sheet1!A24:A25 and named range "desk_1" in sheet2!A24:A25. Is it possible to copy the values in the cells of old_desk_1 to desk_1 without just doing the standard copy/paste?
Yes it is, first I suggest looking at the following documentations.
Google Class SpreadsheetApp
Google Class Spreadsheet
Google Class Sheet
Google Class Range
The above links will tell you about all the methods available for all the classes you will need to do this. Here is a basic example of what you're hoping to acheive.
function copyCells(){
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var theRange = thisSpreadsheet.getRangeByName("YOUR RANGE");
var destinationSheet = thisSpreadsheet.getSheetByName("SHEET TO COPY TO");
var destinationRange = destinationSheet.getRangeByName("YOUR OTHER RANGE");
theRange.copyTo(destinationRange);
}