Javascript and Google sheets interactions - javascript

I'm new in JavaScript and currently building a web apps that interact with Google Sheets as a database.
Basically what i'm building right now is to get input from users through Web apps and giving it to the Google Sheets (which have all the database & formulas in it).
Based on the input, Google sheets will then do the calculation and return back the value to JavaScript to be publish on the Web. (I do this because i want to make the Google Sheets data remain private). Due to the complex dataset, Google Sheet will take some time (around 3-5 seconds) before can pass the value to Javascripts (using SuccessHandler).
The code that i currently have is working ok. But i want to make the Web apps more interactive. What i means is that, during the data processing by the Google Sheets, i want to tell the user to 'hold on' and don't do anything yet. I try to use loader but I don't know how to trigger & to stop the loader (while waiting Google Sheets do the calculation process).
Would greatly appreciate if someone could help me in this problem.
thanks.

Most of the time retrieving data from somewhere else using JavaScript is an asynchronous process. As a result you're not blocking the user from performing actions to keep your site interactive.
The thing you're trying to achieve is keeping track of when your asynchronous data has been collected. This can be achieved with Promises and techniques like async ... await. Another option, though not recommended, is making your code synchronous, meaning the whole page get blocked while your retrieving data from Google Sheets. The pro is that a user can't do anything, the con is that if it takes too long it might look like the page stopped working (as it will literally freeze).
Explaining asynchronous JavaScript is a too in-depth topic to elaborate on here, but there are a lot of tutorials out there to get the hang of this. I'd suggest you dive into that.

Related

Overall flow of a simple script that uses Whoop and Google Sheets API?

I have a spreadsheet I manually fill out every day where I fill in sleep data recorded by my Whoop strap. As a fun project I thought I'd make this automatic and take advantage of the developer tools for Whoop and Google Sheets. Should be noted that I have experience in scientific computing (Matlab and Julia), but absolutely zero web dev experience.
I tried following the instructions in this link which goes through setting up the HTTP request to get sleep data. However, in order to get that to work, I first need to get an access token, so I tried following this tutorial which goes over authentication. I think I know what each of these is doing, but I'm struggling to put it into context of how the overall script will look. Should I just put all those bits of code into a single .js file and just run that? I see terms such as "middleware stack", "routing layer" and "server-side frameowrks" Are these relevant to the presumably simple script I'm trying to implement?
The perfect answer would be directions to any tutorials/books/articles/videos where someone implemented a similar idea just so I get a better idea of how it should all look.
Thanks in advance!

track down google apps script that sends e-mail

I tried looking at other questions and didn't find anything specific enough to help me in my situation.
I have main google sheets that I work off of and I am constantly writing new codes in the script tool to help my team and provide information to our customers. Something that I started doing (which is probably a bad practice in hindsight) is creating new google sheets and using SpreadsheetApp.openByUrl() to manipulate the sheets we work off of without writing thousands of line of code into the original sheets code file. I've been doing this for a while and due to lack of organization, I can't seem to find a google sheet I created that runs through one of my main sheets and sends an e-mail to one of our customers.
I send myself the e-mail as well, so I get the e-mail every day. No where in the e-mail is there anything that traces back to the file that the code is ran off of. I realized that the code never does anything on it's own file, so that file does not get pushed up to the top of my file list when I sort by last edited (in Drive).
Does anyone know of any way I can track down this file? Is there a way to get a list of all code projects I have in my account? Please let me know. Thank you!
Last run in My Triggers (Tools > Script Editor > Edit > Current project's triggers or All your triggers) should narrow you search.
Tools > File Open offers My Projects on the left.
(I don't have the minimum reputation to put a comment)
It sounds like there are a lot of moving pieces going on here. Without narrowing down your search criteria it may be difficult to easily isolate the file in question. It may be possible to put some kind of unique stamp in the emails that can be correlated with the underlying file in question.
You may be able to go to https://script.google.com/home/my and on the right side of the page opening the settings for each project you can click on "Executions" to start digging into the scripts that are running.

use of google script editor

Hey so currently working on my first personal project so bear with the questions!
Currently trying to create a Javascript program that will parse info from google forms to produce slides displaying the info. So far from my research the best way I've found to facilitate this process is googles app script editor. However, I was wondering if I can run this code by requesting it from a different javascript (or maybe even java) program that I will write code on webstorm. If I cant do this what is the best way to utilize the google apps script editor?
Thanks!
Google Apps Script is just javascript with extra built-in APIs (like SpreadsheetApp, FormApp, etc.).
It also has a UrlFetchApp API.
So you can run code like this:
// The code below logs the HTML code of the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText());
As such, if you want to provide JavaScript from elsewhere, you could fetch it and then eval it on the Google Apps Script side. (but we all know how tricky eval can get)
One other option is to have your own server side written using Google App Engine (or any other framework) and use Google's OAuth and authorize your app to fetch data from the Forms form
Slides and Google Apps Script
You might like to take a look at the addon "Slides Merge" by Bruce McPherson. I've never used it but it sounds like it might work for you. Here's what it's looks like in the addon store:
Getting information from Google Forms is a snap with google apps script since your can link the form right up to a spreadsheet. The Google Apps Script documentation is really quite good these days. Here's the documentation link. Google Apps Script is loosely based on Javascript 1.6. If your already a programmer my guess is that you'll have few problems learning to use it. In my experience the most difficult thing was dealing with the arrays of arrays produced by the getValues() method of ranges in google apps script and I made a short video that might be of some help to you.
I also have a script that I wrote in Google Apps Script that produces a sheet show that is a slide show inside of a spreadsheet.
I've found that using the Script Editor is pretty easy. There's some documentation in the support section of the documentation. It can be a bit buggy at times but overall I think it's a pretty good tool.

Can a Google Analytics result be passed to a JavaScript function while the user is still on the page?

On my website, users enter some personal information, including ZIP code. This information will be passed to a function that will determine the display of the next page.
The problem is that the function utilizes an underlying statistical model, for which zip codes have too many possible values (~43,000) to be useful. I want to map zip codes to something broader, like designated market area (DMA has around 200 possible values).
But using Google Analytics and BigQuery, I already have the user's DMA before they even enter their ZIP code. Is there a way to access that information while they are still on the page so I can input it to the function?
In case you are wondering if you can use Google Analytics Information in realtime (not quite clear from the question), that will not work - GA does not work in realtime; data processing time is announced as 4 hours for the "premium" version to 24 hours to the standard version, and even if it's often faster you probably do not want to build you business on an undocumented feature that might or might not work as expected.
Also API limits make realtime data retrieval unfeasible even for smaller sites.
If however you have a stash of precomputed data that can be linked to the current user via an identifier (clientId or similar) it would probably be best to export this to external storage as suggested by Willian Fuks.
Since you mentioned personal data, keep in mind that this must not be stored within Google Analytics as per Google's TOS.
(not quite an answer, more like some thoughts of mine)
I don't think that running queries in BQ for each user you find in production is a good approach.
Costs will increase considerably, performance will not be satisfactory by any means in this scenario and you might start hitting quotas limits for jobs against a single table.
One possibility that might work is having your back-end use some google analytics client for retrieving data from G.A. Still, you should check if the quotas are appropriate for you.
Another possibility (I suspect this might be the best option) that you may consider for your scenario is using Google Datastore. It might suit your needs quite well; you could have some table from BigQuery being exported to Datastore and have your back-end system query it directly for the user DMA.

Google Sheets Automation and Real Time Graphing

My knowledge of Google Sheets is somewhat limited. I have recently been keeping track of daily stock quotes (using the GoogleFinance function) and I was wondering how to utilize Google Sheets script editor to make a more automated and real time graph.
For example, I have columns with the stock price and volume that are updated every so often. I wanted to be able to take the values in these cells as they update and plot them in real time (as a line graph). I know Google script editor on Google Sheets has the ability to trigger at a certain time, so I would like the graphs to start populating at 9AM (when the markets begin to open) and stop populating at 4PM. I figure that updating the graph every 5 or 10 minutes would best enable me to capture the changes.
I wanted to do this to look at the relationships between various metrics (like the fluctuations in price and volume over time). I do not know if Google Sheets is too limited to do something like this, but if you know of a way to do so, please do suggest it.
Thank you!
Well, I think you need to use Apps Script here to achieve this Automation. I found here a tutorial that show how to create an automatically updating spreadsheet. But, sadly to say it did not mention here on how to create graph. But it will give you an idea on how to create automatic updating spreadsheets. You can also check this Google Analytics Report Automation and this thread if it can help you in your case.

Categories

Resources