Import JSON data into single cell in google sheets - json

I am wondering if anyone can give me an example on how to gather data from a JSON file, and import it into a single cell in google sheets? It does not need to be formatted or copied to multiple cells, it simply needs to take the entire contents of the JSON and copy it into a single cell. The file I am working with is also a local file. Can anyone shed some light? It does not necessarily need to use google apps script, if a python script or anything similar could do the same thing that would be ok

First of all, Google Sheets cannot access your local files. It's a web application, so any external data it gets must be accessible from the Internet.
If your goal is simply to put the contents of a web-accessible JSON file in a single cell, the following custom function will do the job:
function import(url) {
return UrlFetchApp.fetch(url).getContentText();
}
This simply grabs whatever page you point at, and crams its contents in a cell. Example: =import("http://xkcd.com/info.0.json")
If you do decide to parse JSON, be advised there isn't anything suitable built into Google Sheets at present. The project importJSON by Trevor Lohrbeer may be helpful.

You can also use an extension of "API CONNECTOR" for free in the google sheets and just paste the url of the json file you need to import.
here is the link to the API Connector extension
https://workspace.google.com/marketplace/app/api_connector/95804724197
here is how to perform it.
https://www.benlcollins.com/apps-script/api-tutorial-for-beginners/

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

how to replicate the document using Google Docs Api?

I need to fetch a simple document template file (.doc) from google docs (or drive), then fill in the missing details and save the document as new file into the cloud.
Could someone advise on how to achieve this using the Google Docs API so this could be done within bespoke environment?
thanks in advance
If I understand you correctly, you want to programmatically:
Retrieve a document from Drive.
Edit this document while keeping the original one unmodified.
Save this new document.
You have to follow these steps:
Use Files.copy from Drive API to copy the file you want to edit. To achieve this, you have to provide the id of the original file as a request parameter. As a response you will get a File resource corresponding to the copy you created. Retrieve the file id from this response.
Update this new file using batchUpdate from Docs API. For that you will need to (1) provide the file id you retrieved in the previous step as a parameter and (2) provide the changes you want to make using the requests parameter (an array of all the changes you want to make) in the request body (check the links I attach to know exactly how should your requests parameter be like - I would like to be more specific here, but I don't know how exactly do you want to modify the document).
Obviously, there is no need to save the modified document, this is done automatically when the file is modified.
I hope this is of any help to you.

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

Creating a Google Apps Script which Automatically Executes on Upload

I have an interesting fairly simple problem I am trying to solve but I am at a loss on the proper code to get this to working correctly.
Basically, I have an very old local application running which allow me to print a receipt of a transaction. I have the ability to enable this receipt to be printed to a specific file within a specific directory on my computer or I can set it up to print out automatically to a printer on my computer. Currently I print the data out to a file, then copy the contents of that file and paste it into a word document template and then manually go through and format the data so it looks clean (I should note that the data prints out in fixed field format requiring a specific font and size so its pasts correctly. After all this is done I then email the word or pdf document out.
Essentially what I am trying to do here is automate this method and I thought this should be possible using a Google Apps Script.
My thought here was that it should be possible to:
save all the original files from my application to a specific
directory on my computer
sync that folder with google drive
have a google apps script automatically execute upon upload
have the google apps script automatically use a specific document
template I create with a header/footer and then paste all the
contents from the new file which was uploaded
have the google apps script pattern match certain elements within the contents of
the document and properly format it.
save the document to a specific location with a specific file naming convention
automatically email the document as a doc or pdf based upon a
specific regex matched field within the source document.
Could anyone provide a specific code example which demonstrates how Google apps scripts can be utilized to monitor the contents of a specific Google drive folder to trigger an event immediately after a new file is uploaded?
Thanks in advance!

Is it possible to read the contents of a Google Apps Script from another Script

I'm looking for a way to read the contents of the script.gs files within a standalone Google Apps Script. I can't find a suitable getAs() Mime format in the Docslist.File service that doesn't fail. The context of this is that I want to use the html service/content service to be able to embed public GAS source samples in Google Sites and elsewhere. (in much the same way as embedding Gist Samples). Any ideas?
It is, in fact, possible. This gets Code.gs:
function myFunction() {
throw ScriptApp.getResource("Code").getDataAsString();
}
There is no way to programmatically retrieve the contents of a ".gs" file. You can only ask for files with ".html" extension and potentially you can return it in a project.
This request has come up in the context of allow local development or Git integration to provide an API way to get/set script source in a file. Please put your comments in this issue tracker request or log a new one if it makes sense .
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1108