How to update data from google spreadsheet while using htmlview - google-apps-script

I have a spreadsheet where I am accepting google form responses and in the same spreadsheet I have a dashboard which give out link to open new form. users are suppose to enter data every 10 minutes and I have a minute counter that show when last time data was submitted, how many minutes since last data submit and what was summary of last submit. Along side this information they will have a link to open new form.
Users are using tablet to record data and they are opening spreadsheet on html view as on tablet browser by default it opens like that as well as its easier to open hyperlink through html view.
I have sheet on recalculate every 1 minute to make sure minute counter updates at least every minute.
But html view does not update it self unless user manually reloads the page hence they cant see updated summary of last submit as well as can not see the timer.
Is there a way to force reload or refresh or recalculate sheet on html view to have this work?
Thank you very much for help in advance.

No, there's no way to refresh the users browser page/session and reload the page, as Apps script can't access the browser.

You can combine window.setTimeout() and window.onload to make a website run a function at intervals.
Mozilla Documentation - setTimeout
Documentation - window.onload
<script>
function reloadMyPage(delayMilliseconds) {
setTimeout("location.reload(true);",delayMilliseconds);
}
</script>
<body onload="JavaScript:reloadMyPage(5000);">
Here is documentation about embedding a Google sheet into a website:
Google Support Documentation - Share a Speadsheet
You need to publish the spreadsheet to the web. Make sure to choose the EMBED Tab:
Copy out the iframe code and put it into your website.

Related

AppsScript with Google Sheets: Conditionally load images based on user interaction/when a range is selected

We've got a google sheet with user editable text that will eventually be used within images. We have a web application that generates the images and serves them, so they can be embedded within the IMAGE function provided within Sheets.
This works fine and ends up being something like:
=IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3) where 'previewText' is simply formatting the string to pass to the web tool.
However, to avoid overloading the server and generating a ton of requests every single time someone loads the sheet, I'd like to make it so images only load in some condition (i.e. a user presses a button or they highlight the text), is there some way of doing this?
Add a checkbox to E2:
=IF(E2,IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3),"Check to preview")
You can also use Apps Script for this!
You will have to start by:
Go to Tools > Create a new script;
Use the following script:
function loadImages() {
let sheet = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("SHEET_NAME");
sheet.getRange("CELL_YOU_WANT_TO_PLACE_THE_FORMULA").setFormula("YOUR_FORMULA");
}
Do not forget to adjust this script such that it meets your needs accordingly.
Insert a new drawing into the sheet;
Attach a script to it by going to the three dots symbol on the drawing; when asked about the name, input loadImages which is the function which we had created previously.
Press the button on the sheet and that's it!
Reference
setFormula(formula).

Use button in Google Sheets to open the pre-filled Google Form from dynamic hyperlink

Issue:
I have a Google Form, let's call it "Stock". The form simply records new entries of stock into the Google Sheet "Stock Form Responses". In this sheet, there is a field called Stock Number which has to be unique. In order to cut out human error in the process of assigning a new stock number to the rows of data added through the form, I have created a very simple formula in a cell:
=max('Stock Form Responses'!B:B)+1
This does exactly what I need it to do, and tells me the latest stock number, and adds one to it, ready for the next addition via the form. I then use this code in another cell (G9 of the sheet called "Control"), to pass that number to the prefilled form link for my form, like this:
=concatenate("https://docs.google.com/forms/MYFORMLINK/viewform?usp=pp_url&entry.465281926=",D10)
This is where D10 from the Control sheet stores the previous calculation of what the next stock number should be.
All of this works great so far, and everything works fine.
Problem:
Google Sheets has this really annoying feature where you can't just click a link in the sheet, you have to hover over it and then click the attached link. It's frustrating, and I'm trying to simplify and clean up steps. I simply want to pass the URL stored in G9 to an action attached to a button, let's call it AddStock. How do I do this?
I've been trawling the internet for hours trying to work it out, and can't seem to figure it out myself (admittedly I have very limited coding understanding, but people keep talking about popup blockers, etc).

Force Auto Submission of Google Form after X Minutes after opening.

I am looking for a way to use Google Forms for tests. I want to be able to send a form link out and have people work on it for X amount of time. After the time period has elapsed I want to have the form auto submitted.
There are a few posts about this same topic but none seem to provide any help.
It's not possible to auto submit a Google Form response after any amount of time after opening. This, because there is not a built-in way to add a script to the response view of a Google Form.

Check that a link has been clicked

I would like to keep track of how many times a certain link in Google Spreadsheets has been clicked. I have been searching but I can't seem to be able to find how to check whether a link has been clicked or if it is possible to attach onclick event on a link.
A quick solution is to use the Google URL shorten service for the links. This page gives you the number of clicks per link.
its not possible, you cant detect cell or link clicks.
Two possibilities here:
One way is to have a server (or an apps script service) be the link "proxy" and redirect to the final site as in:
script.google.com/.../exec?url=myurl.com
your script tallies the hit and then redirects the user. redirecting without an extra click might not be possible with apps script htmlService but its possible on your own server.
you can make the urls look pretty by using =hyperlink in the cells so users dont see the long url (until click)
Another way if you have control of the links you are pointing to is to use analytics on those pages. You can use the referer to tally views that came from a spreadsheet click, or you can explicitly add parameters to each url so that youcan later filter those comming from your sheet.

Google Forms, sidebar for live form

I would like to create a sidebar to a live google form, ideally in order to pick from a (google) map coordinates to be entered in the form.
From the documentation and from my trials I can create a sidebar to the form editor/designer (https://docs.google.com/forms/d/ID/edit), not the live form itself (https://docs.google.com/forms/d/ID/viewform). Forms are different in regard to the Doc and Sheet Apps, that there exist these two views.
For example:
function onOpen() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Maps');
FormApp.getUi().showSidebar(ui);
}
First problem is that the trigger for onOpen() fires when opening the form designer, not when the end user will open the form (/viewform URL) to fill it out.
Then FormApp.getUI().showSidebar(ui); gets the UI of the forms designer and opens up a sidebar there and not in the Form the end user has to fill out.
Documentation seems to confirm this.
So, is it possible to get access to the live form, including any associated triggers?
So, is it possible to get access to the live form, including any
associated triggers?
No
However, you could embed your form in a page that also hosts the map in a separate frame, enabling users to browse the map, click to see coordinates, and then copy them to the form. There would be no interaction between the google-hosted form and the map.