Renaming Google Sheets File Automatically From A Cell - javascript

I'm a complete novice to script, so please forgive my ignorance.
I'm trying to run a script so that when a new sheet is made of a specific set of sheets I use for my clients, that new copy renames itself upon the input of a specific cell (C13) where my clients will enter their name.
I should add I was using the following code that i grabbed from a similar question on here:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var C13 = ss.getRange("C13").getValue();
var name = C13;
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("name");
}
But this only worked for when the sheet opened, and obviously with nothing in C13 this isn't useful
Thank you

If you wish that the sheet gets renamed when a specific range gets edited than you can use onEdit() trigger.
The code below will replace the sheet's name with C13 cell's value if you change it.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = ss.getRange("C13").getValue();
if(e.range.getA1Notation() == 'C13' && name!= '') {
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(name);
}
}

Related

Importrange from multiple URLs using Apps Script

I am a newbie for coding and GAS. I can solve the issue via google spreadsheet quite well, but I am looking for better and efficient ways to solve the problem via GAS.
This is the script I wrote so far.
function myFunction() {
const app= SpreadsheetApp;
const activeSheet = app.getActiveSpreadsheet().getActiveSheet();
const temp = activeSheet.getRange("AE4:AE").getValues();
}
As you can see it from a picture, there are URLs and each URL contains a value which need to be pulled out. I want to importrange values from cell "C6" from each URL and print it to the left column. Key to this problem is that one value needs to be imported from each URL to the left.
Google SpreadSheet
= IMPORTRANGE("URL", "Sheet1!C6")
Puts data in the active cell from Sheet1!C6
function myfunk() {
const ss = SpreadsheetApp.getActive();
const cell = ss.getActiveCell();
cell.setValue(ss.getRange("Sheet1!C6").getValue())
}
Note: this is not a custom function...not meant to be run by placing in cell of spreadsheet.

Hide a sheet when I am active on another sheet

I am trying to automatically hide a sheet called "Add expense" when I am not active on it.
For example.
I have the sheet "BD Expenses", the sheet "BD Tokens", the sheet "BD Income", the sheet "Add expense", the sheet "Add income".
What I want is that when I am active in any of the sheets that is not called "Enter expense", then the script runs completely automatically (without having to click on any button) to hide the sheet called "Add expense".
I currently have this code:
//Ocultar todas las hojas excepto la activa
function Ocultar_formulario_ANADIR_GASTO() {
var sheets = SpreadsheetApp.getActiveSheet();
sheets.forEach(function(sheet) {
if (sheet.getSheetByName('Añadir gasto') != SpreadsheetApp.getActiveSheet().getName())
sheet.hideSheet();
});
};
I have tried various methods but without success.
I know there is a function called onSelectionChange (e) but since I am so new to Javascript I don't really know how to make it work. Hence I have created my code differently.
I have looked at the reference from https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidesheet
I've also googled and stackoverflow, but can't find a solution to this problem.
Currently when running the script from the editor, I get the error "TypeError: sheets.forEach is not a function".
I can't get it to work.
I would really appreciate if someone can take a look at my code and offer me a little help.
Thank you very much.
It looks like there is no specific Google Scripts event for when you change sheets.
But there is a workaround mentioned in the issue tracker ticket - scroll to the bottom of the ticket to see it. It involves using onSelectionChange(e) to track which sheets you moved out of and into.
The following code adapts that workaround to your case:
The code assumes your Google spreadsheet has 2 or more sheets - and one of those sheets is called Secret Sheet. You can change this name to whatever you want (see the first line of the code).
When you move from the Secret Sheet to any other sheet, the Secret Sheet will automatically be hidden.
var secretSheetName = 'Secret Sheet'; // change this to whatever you prefer.
var prevSheetProperty = 'PREVIOUS_SHEET';
function saveActiveSheet() {
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty(prevSheetProperty, activesheet.getSheetName());
}
function onSheetChange(e) {
var sheetToHide = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(secretSheetName);
sheetToHide.hideSheet();
}
function onSelectionChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get current sheet name and compare to previously saved sheet
var currentSheetName = ss.getActiveSheet().getSheetName();
var userProperties = PropertiesService.getUserProperties();
var previousSheetName = userProperties.getProperty(prevSheetProperty);
if (currentSheetName !== previousSheetName) {
saveActiveSheet(); // this becomes the new "previous sheet".
if (previousSheetName === secretSheetName) {
// you have moved out of the secret sheet - so, hide it:
onSheetChange(e); // Call custom sheet change trigger
}
}
}
function onOpen(e) {
saveActiveSheet();
}
The script works by keeping track of which sheet is the currently active sheet - and which was the (different) previously active sheet before that.
It stores the "previous" sheet name in a user property.
You will see that to use the onSelectionChange(e) event, you simply have to add that function to your script:
function onSelectionChange(e) { ... }
Google Scripts automatically recognizes this as an event function. You can read more about this, with examples, here.
I am going to reply to my own question to provide the same solution as #andrewjames but for multiple sheets to be hidden at once.
The code to hide a single sheet can be found in #andrewjames comment.
The code to hide multiple sheets using the code of #adrewjames as a base, is this.
var addFicha = 'Añadir ficha'; // change this to whatever you prefer.
var addGasto = 'Añadir gasto'; // change this to whatever you prefer.
var addIngreso = 'Añadir ingreso'; // change this to whatever you prefer.
var hideMultipleSheets = addFicha || addGasto || addIngreso;
var prevSheetProperty = 'PREVIOUS_SHEET';
function saveActiveSheet() {
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty(prevSheetProperty, activesheet.getSheetName());
}
function onSheetChange(e) {
var sheetToHide1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addFicha);
var sheetToHide2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addGasto);
var sheetToHide3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addIngreso);
sheetToHide1.hideSheet();
sheetToHide2.hideSheet();
sheetToHide3.hideSheet();
}
function onSelectionChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get current sheet name and compare to previously saved sheet
var currentSheetName = ss.getActiveSheet().getSheetName();
var userProperties = PropertiesService.getUserProperties();
var previousSheetName = userProperties.getProperty(prevSheetProperty);
if (currentSheetName !== previousSheetName) {
saveActiveSheet(); // this becomes the new "previous sheet".
if (previousSheetName === hideMultipleSheets) {
// you have moved out of the secret sheet - so, hide it:
onSheetChange(e); // Call custom sheet change trigger
}
}
}
function onOpen(e) {
saveActiveSheet();
}
Here I leave a brief explanation so that if someone needs this code they know how to modify it so that it works for their needs.
1. At the beginning of all of the code, we see the first 3 vars.
In this case we want to hide 3 sheets, that's why there are only 3 vars. In case you want to hide 5 sheets, you should have 5 vars.
Here you can name them whatever you want. Then, the text in quotation marks is the name of the sheet you want to hide, so you must change it to the name of your sheets.
Example:
var hideSheettt1 = 'Here you need to copy the name of your sheet';
2. Following the first 3 vars, we have the 4 var hideMultipleSheets
In this case we are joining the first 3 vars into a single var to be able to reuse it later in the onSelectionChange (e) function
Here you must replace addFicha addGasto addIngreso with the name you have given to your first vars.
Continuing with the example from point 1, we will use the var hideSheettt1 = 'Here you need to copy the name of your sheet'; as a reference.
Example:
var hideMultipleSheets = hideSheettt1 || hideSheettt2|| hideSheettt3;
3. In the SheetChange (e) function you must substitute the var of .getSheetByName () in the first 3 vars, by the name of the var that you created at the beginning of the entire code (point 1).
Example:
var sheetToHide1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(hideSheettt1);
At this point, you should include as many vars as sheets you want to hide and vars you have created at the beginning of your code. In this case we want to hide 3 sheets. But if we wanted to hide 5 sheets, we must have 5 vars, one for each sheet.
We must also have the same amount of hideSheet()

is it possible? onChange from one spreadsheet triggers onChange from another spreadsheet

I have a sheet that when the onchange triggers it makes a mirror of the information to use as a reference later when it determine which rows changed. I had this connected to a Query function. Whenever the query got updated, the onChange function would run. But sometimes the query would act weird and information would appear blank on the sheet it copies to even though i can see it on the query. And if two computers were looking at the query at the same time, it was evident that it didn't update on all screens. So i decided, why not, instead of a query, we have the information get updated the same way the mirror is updated. So it copies all the information down and puts it onto the sheet, no query needed. I figured this would at least make sure everyone is seeing the same information. So i made a query sheet, and added an onChange() trigger to it that grabbed all the info and pasted it onto the main sheet, whenever the query updated. (even if it messes up, at least it's much more obvious and no one is seeing anything different). But when the information was posted onto the main spreadsheet. THAT spreadsheet's onChange function didn't trigger. I thought it would trigger whenever there was a change. No matter what. So I then decided to create a new function in the Query Sheet where it inserts and deletes the last row, even say hi, thinking if I make the sheet change more prominently, the onChange() function would trigger. Still didn't. I'm at a loss.
function onChangeMirror() {
var sos = SpreadsheetApp.openById("the sheet with the query");
var sas = SpreadsheetApp.setActiveSpreadsheet(sos);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getActiveSheet();
var lastRow = s1.getLastRow();
var lastRow1 = s1.getLastRow()+1;
var lastRow10 = s1.getLastRow() +10;
var SRange = s1.getRange(2,1,lastRow,2);
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var sos2 = SpreadsheetApp.openById("the sheet it's updating");
var sas2 = SpreadsheetApp.setActiveSpreadsheet(sos2);
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var s2 = ss2.getActiveSheet();
var MaxRow = s2.getMaxRows();
s2.getRange(A1Range).setValues(SData);
Logger.log(lastRow1)
Logger.log(lastRow10)
s2.getRange('A'+ lastRow1+':'+'B'+lastRow10).setValue(null);
Logger.log(A1Range)
if(MaxRow > '300') {
s2.deleteRow(MaxRow)
}
else if(MaxRow < '301'){
s2.insertRowAfter(MaxRow)
s2.getRange('A'+(MaxRow+1)).setValue('HI')
}
}
here is the script i was using

Send email after new document is created from template that includes URL from new document

I have some script in Sheets that sends an email after a button is pressed letting me know the sheet is newly created. However, the way I have written the script sends me a link to the template, and not the newly created sheet. I want to have the email created include the URL from the new sheet and not the template. I have the script used below (with some information changed for anonymity):
function sendemail() {
MailApp.sendEmail("name#gmail.com", "New Sheet Created",
"The link for the new sheet is
https://docs.google.com/spreadsheets/");
}
Thank you for the help, I plan to check back frequently to answer any questions. If I need to clarify anything, please let me know. Thank you!
I think this answer might be what you are looking for:
https://webapps.stackexchange.com/questions/93305/how-to-find-url-of-the-current-sheet
function getSheetUrl() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ss = SS.getActiveSheet();
var url = '';
url += SS.getUrl();
url += '#gid=';
url += ss.getSheetId();
return url;
}

Sheet name is not updating with the script

In my google spreadsheet i created a script (below) which give me the function/formula to get the name of the sheet/tab which i am currently in.
The problem is when i change the sheet name to a new name then this cell is not updating with the new name even after several refresh.
I have set triggers to run the script as well as i have used SpreadsheetApp.flush() to forcefully applies all pending Spreadsheet changes, but still this does not work.
This is my script below:
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
return s.getName();
}
And this is with the flush :
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
SpreadsheetApp.flush();
return s.getName();
}
Please let me know what changes i need to make so that this can update as and when the sheet name is changed.
This function works when connected to an onChange trigger and the Sheet Name is changed. I think you were adding it to the cell as "=sheetName()". I have to admit I don't know much about custom functions because I always do all of my calculations in arrays because it's much faster. But if you hook this up to an onChange event and set the load value of the sheet name into a cell it works.
function sheetName()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getActiveSheet();
var name=sht.getName();
sht.getRange('A5').setValue(name);
}

Categories

Resources