Google Sheet Static Sheet Name Reference - google-sheets

I have a spreadsheet that contains four different sheets
Day1 | Day2 | Day3 | Summary
in the summary sheet i will be referencing a cell in either Day1,Day2,Day3 sheets
for example i'm using this formula in the summary sheet to get a value from Day3 sheet
='Day3'!$D2
so far its working however i want the sheet name (in this case the 'Day3' in the formula above) to be static.
meaning if i rename the Day3 sheet into something else for example rename it to Day4, and create another sheet named Day3,
i want the formula above to still reference ='Day3'!$D2
because as of now if i do the steps i mentioned above, ='Day3'!$D2 automatically changes to ='Day4'!$D2 when i rename the Day3 sheet
is this possible in google sheets?

This is an example of recalculation in Google Sheets, and for your use case, there is currently no way of disabling this feature.
As a workaround, you can use Google Apps Script to create an installable trigger to change the sheet reference manually everytime the sheet structure changes.
Sample Code:
function createTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("retainReference")
.forSpreadsheet(ss)
.onChange()
.create();
}
function retainReference() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Summary");
var range = sheet.getRange('A1');
range.setFormula("='Day3'!D2");
}
Note: You only need to run createTrigger() function manually.
Sample Output:
When renaming sheet Day3 to sheet Day4:
Reference:
Installable Triggers

Related

Is there a way to use FILTER with multiple Sheets containing a specific phrase in Google Sheets?

I will try to be as clear as possible. Here is the example piece: link
What I want to happen is that the Filter formula will search for any Sheet containing “Form Responses” and then display the results. You can see on the Current sheet how I’ve been doing, but this is more tedious and leads to issues of the first formula begins to overwrite the next one, etc. On the Wanted tab, I’ve laid out how I imagine it and put a note in A7. Any help offered is greatly appreciated!
You can get started with this script:
function getSheetResponses(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Wanted");
var getCellValue = sheet.getRange("A7").getValue(); //Gets the name of your designated sheet on "Wanted Sheet" cell "A7"
var getName = ss.getSheetByName(getCellValue);
var getNameValue = getName.getRange(2,5,getName.getLastRow(),1).getValues(); //Gets all the values of Column E on any defined sheet names based on getCellValue
var datalength = getNameValue.length;
Logger.log(datalength);
sheet.getRange(8,6,datalength,1).setValues(getNameValue); //Puts the data on Wanted Sheet Column F
}
What this does is it gets the sheet name on cell A7, and populates the data on Column F row 8 on the "Wanted" sheet like so:
Now, the data it populates on the "Wanted" sheet came from Form Responses 1 based on the sample piece you have provided:
If ever you would want to relocate which specific row or column the data would be pasted on "Wanted" Sheet. You can refer to this documentation on how to modify the rows and columns on sheet.getRange()
Reference:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow,-column,-numrows,-numcolumns

Running a script on one spreadsheet to change values on another spreadsheet

Looking to run a script on spreadsheet A and change values on spreadsheet B. If I was running the script within spreadsheet B the following works:
function clear() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('Responses!C8:E50').setValue('');
}
How do I change the above script to do the same on a different spreadsheet?
Thanks in advance - Paul
You can use openbyID to change data on other sheet from your current sheet, the ID here is refer to the unique combination of text & number from the sheet URL:
function clear() {
var spreadsheet = SpreadsheetApp.openById('xxxxxx')
spreadsheet.getRange('Responses!C8:E50').setValue('');
}

How to get all page names of a spreadsheet file in a column which will keep updating if the page name is changed?

I have a spreadsheet with say following page names:
"Sheet 1"
"Sheet 2"
"Sheet 3"
Afterwards, I changed name of
"Sheet 1" -> "Rough"
Is there anyway for me to get a list of all page names of the file in a spreadsheet page in a column?
Issue:
You want to trigger an action when a sheet name is modified (sheet here refers to a tab, or to a page, as you seem to call it). Sheets formulas will not get triggered by a sheet name change.
Solution:
You can accomplish this by installing an Apps Script onChange trigger, which will fire a function you specify when the spreadsheet's content or structure is changed (e.g., when a sheet name is changed). To do this, you can follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Create an Apps Script function to do the following: (1) retrieve all the sheet names, (2) write these sheet names to a specified sheet. You could use this function, for example. Copy it to the script editor and save the project:
function onChangeTrigger() {
var ss = SpreadsheetApp.getActive(); // Get current spreadsheet
var sheetNames = ss.getSheets().map(sheet => [sheet.getName()]); // Get sheet names
var destSheet = ss.getSheetByName("All Tab Names"); // Change according to your preferences
destSheet.getRange("A2:A").clearContent(); // Remove previous content
destSheet.getRange(2, 1, sheetNames.length).setValues(sheetNames);
}
Install the onChange trigger to fire the function above, either manually, following these steps, or programmatically, by running this function once:
function createOnChangeTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("onChangeTrigger")
.forSpreadsheet(ss)
.onChange()
.create();
}
Note:
The function onChangeTrigger writes all sheet names to column A of a sheet called All Tab Names. Please change that to your sheet name. Also, check this if you want to change the range to which the sheet names are written.
Reference:
onChange trigger
You could install this Addon Formulas to use one of its functions =UTIL_SHEETNAME() within a cell of all your sheets (let say in A1 of every tab), then you would have a master sheet for listing your tabs and where you reference each formula you've used (=Sheet1!A1; =Sheet2!A1; etc...). But this function doesn't seem to update quickly though.
Alternatively, you could write a code (Apps Script) to list all your tabs, which has been answered everywhere. If you need help with the latter, add in your tags 'Google-Apps-Script' to add more visibility to your question.

Google Sheets - Pushing a row to a second spreadsheet based on a value being entered

I'm new to this,
I have 2 google spreadsheets:
Spreadsheet A: The active sheet Containing multiple tabs with information to be Pushed to B.
Spreadsheet B: A spreadsheet with a single tab. The same headers and structure as spreadsheet A.
Based on the user selecting the answer "Yes" in the first column of any of the 1 tabs in Spreadsheet A, I would like that entire row to move over to Spreadsheet B.
I have modified a script that works on a single spreadsheet (ie moving rows from tab to tab) to attempt to get it to work between spreadsheets:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tss = SpreadsheetApp.openById('B').getSheetByName('Sheet 1');
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(r.getColumn() == 1 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var target = tss.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
}
Probably needless to say, this yields no result. Having searched through a number of posts and forums I only see individuals posting about how to move rows between tabs but not between entirely separate spreadsheets. Is it even possible to do this? If so, what am I doing wrong in the script?
Thank you so much for anyone who takes the time to assist.
Anthony
Following a dialogue with the OP in the comments section, it was indicated that the original spreadsheet did not to be kept secret.
Consequently, the desired functionality can be provided by using a combination of IMPORTRANGE() and QUERY() in the spreadsheet with no need to use Google App Script. For instance,
=QUERY(IMPORTRANGE(url,range),"select A where B matches 'Yes'") or similar
This imports data from a second spreadsheet and then the QUERY() function acts as a way of filtering the imported range by certain criteria.
Once the imported range is authorised, the editors of the spreadsheet can access it by, e.g. removing or modifying the query. You could prevent this by protecting that particular cell, if needed.

How to name sheets based on names typed in cells on summary sheet?

I have very limited knowledge of google sheets and would appreciate any and all help.
I have created a google sheet with multiple sheets. I would like to have the names that are typed in the cells for student names name the following sheets.
Thank you.
Here is a link to the google sheet: https://docs.google.com/spreadsheets/d/1pOOwg5HLGXVg9XynJBGcVWjqnCV6xZSmMX38hZZgmvg/edit?usp=sharing
How about following script?
function sample() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var summarysheet = ss.getSheetByName('Put name of summary sheet name');
var names = summarysheet.getRange('a4:a38').getValues();
names.forEach(function(e, i){
ss.getSheets()[i + 1].setName(e);
});
}
I thought that it is very important the correspondence of names of summary sheet and each sheet. So conditions to use this sample script is as follows.
If you change the arrangement of sheets, the correspondence of names of summary sheet and each sheet cannot be done.
How do you think about the correspondence of names of summary sheet and number of sheets? Now, the number of sheets is 35 + (a summary sheet). This is same to (names of summary sheet) + 1. If you want to insert new sheets using script, please tell me.
It cannot make sheets with same sheet name. So if there are same student names, it is necessary to change a bit.
If you have a script you made for your question, can I ask you about it?

Resources