Last active
April 26, 2024 15:41
-
-
Save jonobr1/45fc5f41a219153aaa18 to your computer and use it in GitHub Desktop.
A node.js script to convert a Google Sheet into a JSON object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple Node.js script to turn a specific page on a Google Sheet | |
* into a JSON object for the main purpose of HTML Templating. | |
* | |
* @author jonobr1 / http://jonobr1.com | |
* | |
*/ | |
var https = require('https'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var format = 'tsv'; // Format you'd like to parse. `tsv` or `csv` | |
var id = 'GOOGLE_SHEET_ID'; // The Google Sheet ID found in the URL of your Google Sheet. | |
var sheetId = 0; // The Page ID of the Sheet you'd like to export. Found as `gid` in the URL. | |
https.get('https://docs.google.com/spreadsheets/d/' + id + '/export?format=' + format + '&id=' + id + '&gid=' + sheetId, function(resp) { | |
var body = ''; | |
resp | |
.on('data', function(data) { | |
body += ab2str(data); | |
}) | |
.on('end', function() { | |
var json = []; | |
var rows = body.split(/\r\n/i); | |
for (var i = 0; i < rows.length; i++) { | |
json.push(rows[i].split(/\t/i)); | |
} | |
fs.writeFileSync(path.resolve(__dirname, './sheet.json'), JSON.stringify(json)); | |
console.log('Generated sheet.json'); | |
}); | |
}); | |
function ab2str(buf) { | |
return String.fromCharCode.apply(null, new Uint16Array(buf)); | |
} |
I'm not totally sure of this, but I believe the sheets API has changed and you need to oauth in now? (link)
So I don't think this code will work anymore.
I slightly change the implementation with google api key (now required for public sheet as well )
Google developer console - > library find sheets enable it.
let url = "https://sheets.googleapis.com/v4/spreadsheets/" + id + "/values/" + sheetId + "?key=" + apiKey;
https.get(url, (resp) => {
let body = "";
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
try {
let json = JSON.parse(body);
// do something with JSON
} catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.error(error.message);
});
Nice!
getting this [["{\n "error": {\n "code": 400,\n "message": "Unable to parse range: 0",\n "status": "INVALID_ARGUMENT"\n }\n}\n"]]
when using this url
let url = "https://sheets.googleapis.com/v4/spreadsheets/" + id + "/values/" + sheetId + "?key=" + apiKey;
I could be wrong, but I believe the latest version of Google Sheets does not allow you to access spreadsheet content in this way anymore.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey,
This is working perfect earlier, now google changed their policy I guess, I am getting below error now with this code. I had taken public sheet to test.