Generate HTML View from Google Sheet using Apps Script - google-apps-script

I need to generate/display an HTML page (or iframe or how-ever it could be done on demand from an apps script) from a Google sheet using Apps Scripts. It looks like there's an htmlForms service for Docs, but I can't find the equivalent for sheets.
My goal is to provide a menu item that can parse my data (already done) then call up another page where knockOutJs will be used to transfer the generated JSON into an html preview. Right now my script generates the JSON and if I could use something like
I'm using HtmlService.createHtmlOutputFromFile() to generate the HTML but FormApp.getUi() isn't valid in this context and neither is

You must get a reference to the current SpreadSheet (not the "ActiveSheet", but the entire SpreadSheet object) then call show() passing in your object from createHtmlOutputFromFile() or similar method.
var htmlRes HtmlService.createHtmlOutputFromFile('YourProjectHtmlFile');
SpreadsheetApp.getActiveSpreadsheet().show(htmlRes);

Related

How to access and extract data in App Script that is not bound to the code

I am working on a script that is standalone. It collects its baseline data from a google sheet bound to a form. In the form submission, excel documents are uploaded. While I can access the link for the excel document(google downloads the excel into your drive upon submission and inserts the drive link into the google response sheet), I am having difficulty looking into and extracting anything (exact cell values, ranges, indexes, etc) from the excel doc.
I have tried using various functions from the sheets and spreadsheets classes and continue to get errors. Some suggestions I've found say the excel document needs to be converted into a google sheet before the app script can access it, or that app script won't allow you to work with such a document if it is not bound to your script (since that allows you to activate it)(I can't bind the excel doc because it changes upon every new submission of the form).
Has anyone ever compiled a similar code and figured out how to access an unbound, non-google doc?
Let me know if you need error codes or script snippets. I just wasn't sure if this was a syntax problem or a google suite trick spot that needed extra code that I probably haven't found cause I'm new to this platform.
From the question
Has anyone ever compiled a similar code and figured out how to access an unbound, non-google doc?
Scripts in Google Apps Script aren't "compiled" by the script writer in the sense that it's done when developing something in other platforms.
To get data from a file hosted in Google Drive, first the script should get that file by using the Drive Service (Class DriveApp) or the Drive Advanced Service.
The next part depends pretty much on the Excel file format. If it's an xlsx file, then usually the most convenient is to convert the file into an Google spreadsheet as this will make possible to use the Spreadsheet Service (Class SpreadsheetApp) to read the data from it.
If you don't want to convert it to a Google spreadsheet file, or it can't be converted the the "basic" means because it's using an incompatible format (like a xls file format), then you will need to use an library or an external service to parse the Excel file content.
function getalldataonsheet() {
const ss=SpreadsheetApp.openById('ssid');//you provide id
const sh=ss.getSheetByName('sheetname');//you provide sheet namme
const rg=sh.getDataRange();
const vs=rg.getValues();//2d array
return vs;//this return 2d array
}
There are restrictions for passing parameters in client to server communication

On a Google Sheets, is there a way to trigger a Google Script function from a standalone webpage <button>?

I've created a small electron app that shows data from a google sheet via sheetrock.js to display table data. I've added a form that submits data to the google sheet with the help of triblondon's git
Now I'm trying to Implement a simple button on the electron app that will move populated rows from one sheet to another, creating a history. But for the life of me, I can't find a method of doing so that doesn't involve sending json data like the method above, writing to a cell and just doing formulas from there.
I'm just looking for a simpler, more direct approach by just invoking a function on the google script side (button + javascript probably).
Thanks for any help you can provide.
You can deploy your script as apps script. You will have use the public URL to be triggered by your button.
You can check everything about it on the Web Apps documentation.

IMPORTxml on google sheets

Trying to make a spreadsheet on google sheets that scrapes data from a site.
I'm trying to get the sell price of items from rsbuddy exchange, eg:
https://rsbuddy.com/exchange/?id=1745
I have the code on google sheets as:
=IMPORTxml("https://rsbuddy.com/exchange/?id=1745","//*[#id='sell-price']")
But instead of showing me 1734gp it comes up as --- on the sheet.
I've tried adding /text() at the end of the query for the importxml but it doesn't change anything.
I'm guessing the solution is something similar?
I don't believe you can do it with xpath because it's populated dynamically. If you view the full source, you're getting what is there. It's literally ---
You can see the source data here which is in JSON and looks like it's tied to the page url id. Google sheets doesn't natively support json but this good person wrote a script and it seems to work well in my example sheet.
You'll need to do it via JSON, though you can't importJSON natively through Google Sheets. If you add this library as code to your sheet (via the script editor) then you can you can use =IMPORTJSON (with different parameters) to get the data back that you need.
http://blog.fastfedora.com/projects/import-json

How can I pass a variable to spreadsheet function?

Here is what I'm trying to do:
Embed a Google spreadsheet in my website in multiple locations. For registered users, I want to show all the data in the spreadsheet. For unregistered users, I want to hide key numbers.
I have a spreadsheet with a custom function called blackOut(). I've put the blackOut function in all of the cells with sensitive data. All blackOut() need to do is this:
function blackOut(e) {
var is registered = ??
if (is_registered) return e;
else return "";
}
But I cant find any way to 'pass' any variables from my website to the function, so I haven't been able to make it work.
Here's my environment:
In PHP I'm using cURL to get the spreadsheet's embed code from this URL:
https://docs.google.com/spreadsheet/pub?key=[spreadsheet ID]&output=html&widget=true
then I'm doing a few str_replace functions to make the JS and CSS work.
Here's what I've tried:
using window.location in my function (doesn't work. Google doesn't let you access window)
passing a javascript variable in globally (in JS global variables are technically attached to the window object)
using SpreadsheetApp.getActiveSpreadsheet().getURL() (just returns the URL for the spreadsheet, not the one that existed in the http request)
looking through all of the available objects in Google Code
My Ideal Solution:
It would be great if I could just add '&blackout=true' to the URL, and pass that into blackOut() in the google script. I've looked around a lot, and I don't think it's possible.
I've looked at these links:
How can I get URL parameters passed to a Google Spreadsheet using Google Apps Script?
Alternative to global variables
GAS: Problems in using global variables in functions
Any ideas on how I can get this to work?
Maybe i'm understanding your question wrong, but am i to understand that you are not using a iframe or viewing the sheet directly? You are using a published url and from there you get your data. You already do some custom conversion to the HTML (so you have the knowledge and tools to edit your data/html yourself).
Could you go a small step further and create the whole html table yourself?
Then you could use the query functionality of google spreadsheets (url query parameters).
This way you could validate the users from your site and do two different data query's.
One with all the columns, one without some columns.
For example see: http://acrl.ala.org/techconnect/?p=4001
For the google language reference: https://developers.google.com/chart/interactive/docs/querylanguage

Calling a google script function in html page

I've created a function in Google Script Editor that appends a row in Google Spreadsheet. Values being passed as parameters.
Now, what I want to do is, call this function in my HTML page (built using UI Services, not HTML Services) and pass the values to be stored from this page. I cannot find a way. Pls help?
At the time you asked this question, the documentation was not very rich. It has improved considerably, and there are examples provided that are close to what you want to do.
With the UI Service, the way you would accomplish this would be by using a handler function, either client-side or server-side, which would be triggered by a button click (for example), and handle the submission of a "form" (a set of input text areas, etc.). The handler would call your storage function, passing the values returned from the form.
See the examples in the documentation links provided above.
If your web app used Html Services, you would have several options for calling server-side apps-script, see Html Service: Communicate with Server Functions. There are also numerous questions here that demonstrate this:
Call a Google Apps Script function in HTML
HtmlService form submit opens new tab with foo bar URL
Short answer:
google.script.run.nameOfYourFunction();
Use this to call your function that is defined outside of the html file, from within the html file. Based on the docs