The Specified GSHEET_JSON_URL does not contain JSON - javascript

I have a working Google Sheet JSON search engine here:
https://codepen.io/Teeke/pen/gOwgvXQ?editors=0011
It reads this Google sheet:
https://docs.google.com/spreadsheets/d/1c2aJmDdLkbjW0ErfUB0sfiQ-pt6zdW5KWZgREWs0zvM/edit#gid=0
I made an exact copy of the spreadsheet, and didn't change anything:
https://docs.google.com/spreadsheets/d/1WUzfGeyOMVVOb8tbYLqHTrAk7Ii2p6l58EXG-kcgOcY/edit#gid=0
The codepen with the new spreadsheet raises the following error:
The specified GSHEET_JSON_URL does not contain JSON:
https://codepen.io/Teeke/pen/zYKNRpZ?editors=0011
$(function() {
var GSHEET_JSON_URL =
'https://spreadsheets.google.com/feeds/list/1WUzfGeyOMVVOb8tbYLqHTrAk7Ii2p6l58EXG-
kcgOcY/1/public/values?alt=json';
The urls look exactly the same.

Try to access your Google Sheets from an incognito window. Your first sheet is published while your second sheet is not.

Related

XmlService using Apps Script is finding an error with the '&' symbol

I have the below function trying to convert a webpage to xml, so I can start extracting out some data from tables etc.
function getWebpageContent() {
var url = "https://training.gov.au/Training/Details/BSBCRT501";
var xml = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(xml);
Logger.log(document);
}
I'm recieving this error:
Exception: Error on line 170: The entity name must immediately follow the '&' in the entity reference.
getWebpageContent # Code.gs:6
When I search that webpage for the "&" symbol, (assuming that XmlService is confusing the 'and' symbol for some sort of html code and throwing an error) I can only find one hidden one. And am not sure how to circumvent it.
Any way to dodge that error and get the webpage info as Xml in Apps Script?
From your following replying,
The output I want from this page (https://training.gov.au/Training/Details/BSBCRT501) is each in the 'Elements and Performance Criteria' table. I want to save it as an array to then reformat into my spreadsheet. I might just use IMPORTXML in a spreadsheet formula instead.
In this case, how about the following formula?
Sample formula:
=IMPORTXML("https://training.gov.au/Training/Details/BSBCRT501","//table[2]//tr")
Result:
Reference:
IMPORTXML
Added:
From your following replying,
That's a great answer thanks and the method I think I'll use. It doesn't link break at each point (2.1, 2.2 etc) unfortunately but it's still good. I don't think I can accept it as the answer though as it doesn't solve the specific Apps Script problem, but thanks a lot for this.
I added a sample script for using Google Apps Script. Could you please confirm it?
Sample script:
Before you use this script, please enable Sheets API at Advanced Google services. When you run this script, the table is put to the active sheet.
function myFunction() {
const url = "https://training.gov.au/Training/Details/BSBCRT501";
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (res.getResponseCode() != 200) throw new Error(res.getContentText());
const table = [...res.getContentText().matchAll(/<TABLE[\s\S\w]+?<\/TABLE>/g)];
if (table && table[1][0]) {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getActiveSheet();
const resource = {requests: [{pasteData: {html: true, data: table[1][0], coordinate: {sheetId: sheet.getSheetId()}}}]};
Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
}
}
References:
Class UrlFetchApp
Method: spreadsheets.batchUpdate
This guy here explains the underlying cause, and also gives the solution:
The entity name must immediately follow the '&' in the entity reference

Contents persistence in google script "blob"

I'm putting togheter some lines of code in Google scripts. The important piece is (consider true the fact that I have a good mySpreadsheet with contents, also source and folder are set):
[...]
var mySpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(source.getId()).makeCopy("test", folder))
[...]
var sheet = mySpreadsheet.getSheets()[0];
sheet.clear();
//email, obj, msg are string set before
sendPdf(mySpreadsheet, pdfName, email, obj, msg);
}
function sendPdf(sheet, pdfName, email, object, message) {
//save to pdf
var pdfFile = sheet.getBlob().getAs('application/pdf').setName(pdfName);
// Send the freshly constructed email
MailApp.sendEmail(email, object message, {attachments:[pdfFile]});
}
The script works, it means that the script creates the pdf and attach it to the email.
I expected, after cleaning the (only) sheet of the file, to receive an empty pdf.
mySpreadsheet is empty, but the pdf attached contains all the contents copied from source. Where these data came from? Where are they "stored" when the sheet is being cleared? Thank you in advance.
In your situation, you want to receive the Spreadsheet using sheet.clear() for the 1st sheet as an attachment file of an email.
If my understanding is correct, how about this modification?
Modification point:
Although I'm not sure about your whole script, after sheet.clear() is run, it seems that the Spreadsheet is not saved. I thought that this is the reason of your issue.
By saving the Spreadsheet after using sheet.clear(), the blob can be retrieved from the updated Spreadsheet.
Modified script:
Please modify as follows and test it again.
From:
sheet.clear();
To:
sheet.clear();
SpreadsheetApp.flush(); // Added
Reference:
flush()
If I misunderstood your question and this was not the result you want, I apologize.

Mapbox data with google sheets

I am working to develop a crowdsourcing website that takes input from google forms and displays this information on a map. I am working with mapbox and am looking for a way to get the data points in my google spreadsheet (with lat/long info) to automatically show up on the map. Any tips would be appreciated, thanks!
I'm working on a similar project.
I 'published' my google spreadsheet: file > Publish To Web.
You should get a popup window with a URL for your sheet:
https://docs.google.com/spreadsheets/d/[your ID here]/pubhtml
Once you have the ID you can use it on your website:
// ID of the Google Spreadsheet
var spreadsheetID = 'fakeExample';
// Make sure it is public or set to `Anyone with link can view`
var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/od6/public/values?alt=json";
$.getJSON(url, function(data) {
var entry = data.feed.entry;
$(entry).each(function(){
//do stuff with each entry in your spreadsheet
// for example, build a GeoJSON object with your lat/lons or add an individual marker
// ex: add the 'country' column to the `results` box
$('.results').prepend('<h2>'+this.gsx$country.$t+'</h2>);
});
});
I suppose you could use any mapbox js map with omnivore, for example.
The form results go in a google sheet - geocode it automatically with some script - publish the sheet to the web with results in csv - the coordinates are pulled automatically to the map which updates upon loading.

Google Apps Script Bad Value

I'm getting a "bad value on line 4"... I don't know why. I'm trying to make a Google sheet that automatically opens to an assigned tab based on gmail address for a large team. Please help!
function onOpen() {
var email = Session.getActiveUser().getEmail();
var username = email.slice(0,-9);
var ss = SpreadsheetApp.openById(username);
SpreadsheetApp.setActiveSpreadsheet(ss);
}
I suspect here your issue is a misunderstanding of the function '.openById()'.
This function is designed so that you identify and open the spreadsheet using a spreadsheet ID (The alphanumeric part of the URL when opening a sheet, such as "abc1234567"). From context and your use of the variable 'username', I think that instead you're somehow trying to open it based on an email ID (Such as user#domain.com).
Incidentally, you won't be able to open the sheet in an assigned tab using Scripts. That's not what it does, and it's unable to manipulate a users browser. Perhaps an extension for Chrome would be closer to what you're looking for.

How to retrieve data from Google Spreadsheet to Javascript or JSON?

This is public spreadsheet created using Google Drive:
https://docs.google.com/spreadsheets/d/1hA4LKZn9yKoqnSzaI6_73GQSj_ZVpB3O0kC93QM98Vs/pubhtml
How to retrieve data from Google Spreadsheet to Javascript or JSON with new Google Spreadsheets API version 3.0 ?
You can access a cell-based basic feed using the following URL structure: https://spreadsheets.google.com/feeds/cells/1hA4LKZn9yKoqnSzaI6_73GQSj_ZVpB3O0kC93QM98Vs/od6/public/basic?alt=json . By default the feed is in XML form, however, you can request JSON format using alt=json query parameter.
This feed also supports JSONP requests, so an example of fetching this data from a browser with jQuery might look like:
var url = "https://spreadsheets.google.com/feeds/cells/1hA4LKZn9yKoqnSzaI6_73GQSj_ZVpB3O0kC93QM98Vs/od6/public/basic?alt=json";
$.ajax({
url:url,
dataType:"jsonp",
success:function(data) {
// data.feed.entry is an array of objects that represent each cell
},
});
If, alternatively, you want to keep it all in the Google environment. if you're looking for a more controlled JSON generator, check out this gist:
JSONPuller
It takes in a Spreadsheet sheet and returns an array of objects, with the row that you decide as the key (defaults to whichever row is frozen)
Cheers,
I made it work using opensheet.
Steps:
Make sure the sheet as read access - Anyone with the link can view it.
Sheet should have First row as Header row with titles
Then get the sheet data as json using the following link - https://opensheet.elk.sh/spreadsheet_id/sheet_name
Replace spreadsheet_id with your sheet id and sheet_name with your sheet name. No authentication is required.

Categories