Created
July 6, 2018 17:41
-
-
Save wegorich/588807daef4e54d357efb1b0695f828a to your computer and use it in GitHub Desktop.
how to crawl google docs using node.js
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 | |
* | |
*/ | |
/* eslint func-names: ["error", "never"] */ | |
const https = require('https'); | |
const { StringDecoder } = require('string_decoder'); | |
const format = 'tsv'; // Format you'd like to parse. `tsv` or `csv` | |
const sheetId = 0; // The Page ID of the Sheet you'd like to export. Found as `gid` in the URL. | |
function downloadSheet(id = '1bBeofyj-HVGEqetcjwOnSHN_m8bNDYgV48afBTpMrPw') { | |
return new Promise(res => { | |
const url = `https://docs.google.com/spreadsheets/d/${id}/export?format=${format}&id=${id}&gid=${sheetId}`; | |
https.get(url, resp => { | |
const decoder = new StringDecoder('utf8'); | |
let buff; | |
resp | |
.on('data', data => { | |
buff += decoder.write(data); | |
}) | |
.on('end', () => { | |
const json = []; | |
const rows = buff.split(/\r\n/i); | |
for (let i = 0; i < rows.length; i++) { | |
json.push(rows[i].split(/\t/i)); | |
} | |
res(json); | |
console.log('Generate translations.json'); | |
}); | |
}); | |
}); | |
} | |
exports.get = downloadSheet; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment