google sheets clear some cells on page load - google-sheets

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

Related

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

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.

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.

Bringing in a google form response to a sheet

Here is a copy of the sheets and form (form > go to live)
https://docs.google.com/a/ncsu.edu/spreadsheets/d/1mEz9mLEgP4Cfts2ZU6NwhNiL6xjWdFbt42RUj-RYbZw/edit?usp=sharing
So currently, I get responses in a different sheet. Ideally, I would get the response on the tracking sheet with the appropriate format for the dates. Additionally, I would like to keep the functionality of the tracking sheet which is to be able add in items by hand and not through the form. I currently have a script in place which auto-inputs the current date as the two separate formats.
So I'm looking for a solution which either takes in form response to the sheet and appropriate cells (the first empty cell). Or I'm looking to create a script which onEdit which when the form sheet is edited, will fetch the new response and copy it over appropriately onto the tacking sheet.
I have tried googling answers.. But I think I may be a failure at it since I wasn't able to really find a solution for this particular problem. Thank you in advance for the help!
form:
https://docs.google.com/forms/d/1AHSQrLkq3U7NJMvgWoH3lAygova7Mi4BaXHdROaaS2g/edit?usp=sharing
The script associated to this form should do the trick.
All the explanations on how to use it are in the response sheet

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