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);
}
Related
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.
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);
}
}
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()
I created a script to create a new folder every time a form is submitted. The point of the script was to put each newly created folder in a specific parent folder determined by a column value on a spreadsheet (new values are generated when forms are submitted). At the moment the script does not fire automatically even though a trigger event is setup, but will fire and create the new folder in the proper location when manually run. Any help would be appreciated.
function createNewFolder() {
// identify the sheet where the data resides
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Form Responses 1");
var ids = ss.getSheetByName("Form Responses 1");
//identify the cell that will be used to name the folder
var getName = names.getRange(names.getLastRow(), 3).getValue();
//identify the cell that determines which parent folder to use
var folderId = ids.getRange(ids.getLastRow(), 5).getValue();
//identify the parent folder the new folder will be in
var parentFolder = DriveApp.getFolderById(folderId);
//create the new folder
var newFolder = parentFolder.createFolder(getName);
}
The trigger fails about 85% of the time and is setup to fire when a new form response is logged on the associated spreadsheet.
Try replacing your onFormSubmit function with this:
This function should be created in the spreadsheet that has the linked sheets. This function utilizes the data that is sent in the form submit event object. Don't forget to include the 'e' in the function as a parameter.
function createNewFolder(e) {
var parentFolder = DriveApp.getFolderById(e.values[4]);
var newFolder = parentFolder.createFolder(e.values[2]);
}
Event Object Form Submit
Also you may want to look at this question since we have been seeing a lot of problems with spurious form submit triggers. If you don't need to implement it fine but it might be something to consider.
It's most likely an async issue. The parentFolder.createFolder function probably is getting called before the parentFolder is done fetching data from DriveApp.
Try this:
var newFolder = parentFolder.then(() => {
parentFolder.createFolder(getName)
})
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