Running a script on one spreadsheet to change values on another spreadsheet - google-sheets

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('');
}

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

How to use a value to search a tab and get a value from the new sheet in googlesheet

Hello i'm using google sheet as my second databse and in the main page called Companies i have a list of companies as shown below:
and i wrote a function that generate a new tab for every companie in the first column. here is what a tab looks like
my goal is in the companies tab under "Workers" i want to get the value of "Total workers" of each companie. the list of companies will be constantly growing so i thought about maybe a function that uses the value of the first column to search for the tab and then get the value of G2.
I am really new to google sheet and i would appreciate any help on how to solve this problem
SUGGESTION
You can try this sample script below with custom function named getTotalWorkers & then add it as a bound script to your Spreadsheet file:
UPDATED Script:
function getTotalWorkers(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var names = ss.getRange("Company!A2:A").getValues().filter(String); //get names of the sheets on column A
var res = [];
for(x=0; x<names.length; x++){
var data = ss.getRange(names[x]+"!G2").getValue(); //get the current cell G2 values on every sheet tabs
res.push([data]); //place all values to a tem[orayr array variable
}
ss.getSheetByName("Company").getRange(2,6,res.length,1).setValues(res); //add the values under the "Workers" column on Company sheet tab
}
Sample Demonstration
After saving the script from the Apps Script editor, place the updated getTotalWorkers function to a time-driven trigger:
The time driven trigger will auto populate the "Workers" F column cells every minute (based on my sample time-driven trigger configuration):

How to pin a common column on all the sheets

I am building a google sheet spanning multiple sheets. I have an index page with hyperlinks to few select sheets for convenience.
I'm looking to pin this column to show up on all the sheets.
Most suggestions I found online is about copying over the column from index page to all sheets, which I find hard to maintain and scale.
Is there a better way to achieve this?
Suggestion:
Perhaps you can add this sample bound script below to your spreadsheet file then save & run it from the editor:
To create a bound script in Google Sheets, open your spreadsheet and click Extensions or Tools > Apps Script or Script Editor
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var indexPage = ss.getSheetByName("Sheet").getRange("A:A");
var sheets = ss.getSheets();
sheets.forEach(cursheet => {
indexPage.copyTo(ss.getSheetByName(cursheet.getName()).getRange("A:A")); //Copies index on every sheets' column A
ss.getSheetByName(cursheet.getName()).setFrozenColumns(1); //// Freezes the first column on every sheet
});
}
This script will also automatically run every time you edit (that means every time you press Enter key after editing/adding a new cell value) your spreadsheet file as it was configured with onEdit trigger
Sample Result
After saving & running the script from the Apps Script editor:
Sheet:
Wish List
Laptops
Apparel
Reference:
Google Apps Script
Apps Script: setFrozenColumns(columns)

Google Sheet Static Sheet Name Reference

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

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