Triggering a script on a google sheet after editing a separate sheet - google-sheets

Hello, I am making an initiative table for D&D using google sheets.
At the moment I have a Mainsheet in which myself (the DM) can edit and change, and Playersheet that has the data the players need using IMPORTRANGE.
I am attempting to write a script that will automatically hide all empty "initiative" cells in PlayerSheet .
I have successfully coded this script, however I would like it to trigger any time I edit the MainSheet.
Because all the date in the sheet comes from using IMPORTRANGE I am not able to use the on edit trigger, as the PlayerSheet is never edited.
Is it possible to have the script located in PlayerSheet run whenever I edit MainSheet?
On top of that would it be possible to have the script only run when I edit data in the F column?

To run the script whenever you edit your spreadsheet, you can create a fonction on edit.
function onEdit(e){
var range = e.range;
//here you can check if the column if F or not and run your code.
}
Tu change the values of another spreadsheet, when selecting the spreadsheet you want to do the actions on, instead of selecting the active one, you can edit one with the url. Something like :
var doc = SpreadsheetApp.openByUrl("paste the url here");
var sheet = doc.getSheetByName("Name of the sheet");
etc...
And then do the modifications you need to do on the players spreadsheet.

Related

google sheets clear some cells on page load

Is there any way to delete the values of some cells in google sheets when the user opens the sheet?
In fact I am using google sheets as a form and want the form to do some calculations. But when the user open the sheet I want to clear it. Is there any way to clear cells on page load?
From Extensions and then App script I add below code and saved it. Then it worked.
function onOpen(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName('your sheet name');
sheet.getRange('G2:G18').clearContent();
}

Google Sheets - Autoreplace a value when a new row is created

I use Zapier to create a new row in a Google Sheet doc for each new registration on our website. However, I want to autoreplace a value of a cell when the row is created or, if it's not possible, using a script with onOpen, or any time-based trigger.
Here is an example of the data that needs to be changed in two specific columns (say, A and C)...
/*Job - Column A - English to French*/
'Management':'Gestion',
'Social work':'Travail social',
'Reception':'Accueil',
'Art Therapy':'Art-thérapie',
'Volunteer work':'Bénévole',
/*Workplace - Column C - English to French*/
'Starting Community':'Communauté en démarrage',
'CLAN Program' : 'Programme FER',
'Other (specify)' : 'Autre affiliation',
'University (specify)' : 'Université',
How can I do that? I managed to use an onEdit trigger, but since it can't be activated by Zapier, it's useless.
Thanks!
You cannot achieve this behavior with an Edit trigger in Apps Script.
Zapier will edit the Spreadsheet via API call and this is actually one of the trigger limitations:
Script executions and API requests do not cause triggers to run. For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.
You should probably craft this auto-replace feature in the Zapier environment directly.
Refer to this documentation for additional Apps Script triggers limitations.
The workaround I use for having automatic functions applied on the row created in a Google Sheet by Zapier is by doing the automation with Google Sheet functions. Your document should have 3 tabs:
Where Zapier will create new rows
On this tab, you will use the IMPORTRANGE function (to import all the data from the tab1).
Where you can do your magic (functions doing exactly what you want, using reference to the cells of the tab2).
Optionally, you can then hide the first 2 tabs, once your setup is complete on the tab3.

On Form Submit, How do I update another sheet with some of the new information?

So I am essentially trying to do what Zapier does with it lookup row and update row as needed. I can't use Zapier for our team's purposes because I can't work with the 15 minute ish delay.
1 - User sends data via form to Sheet A.
2 - User then needs to update information in Sheet A by submitting new data to sheet B via another Form.
3 - Data in sheet B is then matched up with row data in sheet A via the "ID".
4 - Sheet B Data then overwrites Sheet A data.
I feel like this should be simple...
Try using Forms Service, Spreadsheet Service, Triggers and Events
Form Service
This service allows scripts to create, access, and modify Google Forms.
Spreadsheet Service
This service allows scripts to create, access, and modify Google Sheets files.
Triggers and event
Triggers let Apps Script run a function automatically when a certain event, like opening a document, occurs. Simple triggers are a set of reserved functions built into Apps Script, like the function onOpen(e), which executes when a user opens a Google Docs, Sheets, or Forms file.
Using Form submit trigger you can catch the form values using ItemResponse method, get the details from the previous sheet using getValues(). Compare the two sets of values then make neccessary updates.
Hope it helps.

How to get a specific URL of Google Sheet automatically

I would like to get Google Sheet's URL automatically when there is a specific no. in row A and a sheet name provided as prerequisites.
If there is 15106 in row A and a sheet name --> See this
I would like to display it somewhere on the same line with the number.
You can try writing an onEdit() function that gets the URL when the sheet is edited - you can tell the function to look for specific ranges and values as you desire. You will have to assemble the url yourself using spreadsheet ID, which you can get using SpreadsheetApp.getActiveSpreadsheet().getId(). See Google's guide to triggers for more information on onEdit().

Refresh ImportHTML

I have a sheet that is importing data using ImportHTML. The data changes quite frequently, is there any way to have Google Sheets automatically refresh the data?
I tried adding & year(now()) & month(now()) & day(now()) & hour(now()) & minute(now()), to the formula but I get the following error:
This function is not allowed to reference a cell with NOW(), RAND(), or RANDBETWEEN()
"Any way"? Yes, but as Mogsdad has mentioned 'round these parts before, "Since the retirement of the GOOGLECLOCK function, there is no formula that will trigger automatic recalculation."
Using Google Apps Script and something like...
function getData() {
var queryString = Utilities.formatDate(new Date(), "GMT", "yyyyMMddHHmmss");
var cellFunction = '=IMPORTHTML("<your-url>?' + queryString + '","<your-query>",<your-index>)';
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("<your-sheet>").getRange('<your-cell>').setValue(cellFunction);
}
...should get you there. After saving, naming, and authorizing the script, set up a trigger so it runs/refreshes the data as frequently as you'd like.
Very late to the question. I recently had to do a similar thing. I thought I'd write something up here for others to find useful..hopefully.
My Use Case:
I wanted my google sheet to call IMPORTDATA on a specific url every few minutes because the data stored against that url was being changed by a Timer Trigger and an Http Trigger Azure Function.
Basically, I wanted my google sheet to get refreshed every 5 minutes because I knew the data against the url keeps changing.
What I Tried:
I started out with a similar script to what #greg alluded. (Cheers for that.)
I setup a timer trigger for that script to be 5 minutes. All looked hunky-dory.
Issues:
I realised that setting the function as a value on the cell caused the function to run only at the start when the sheet was empty. The second time the trigger hit, the sheet did not get refreshed.
So I tried
cell.setValue("");
cell.setValue('=IMPORTDATA("my-url")');
No Luck.
The sheet did not get refreshed with the new data. It seemed as if the sheet saw that the value-to-set in the cell is the same as the existing value, so it did not bother re-running the function.
To confirm my hypothesis,
I manually tried:
Cut the IMPORTDATA function from the google sheet cell.
Paste the value back into the cell.
The sheet did not get refreshed.
But when I manually tried:
Remove/Change the url from/in the cell. Hit Enter.
Paste the formula again in the cell. Hit Enter.
This worked. So, it seems that cell.SetValue() or even cell.setFormula() just doesn't re-run the function I was setting because the same function existed in the cell to start with.
My Solution best suited to my use-case:
Since the data against the url, I was pointing to, was already in CSV format,
I did something like this:
var firstSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var text = UrlFetchApp.fetch(url).getContentText(); //gets the data.
var csv = Utilities.parseCsv(text); //parses the data.
var range = firstSheet.getRange(1, 1, csv.length, 17) //getting the range.
range.setValues(csv); //setting the data.
I'm basically grabbing the data from the url and putting it into the sheet on timer instead of calling the IMPORTDATA function since that approach didn't quite work out for me.
You could do a similar thing, assuming you want to get some sort of table or chart content and save the values to your sheet.

Resources