Skip to content

Commit

Permalink
fix sheets example given Sheets API change
Browse files Browse the repository at this point in the history
  • Loading branch information
Irene Alvarado committed Mar 7, 2022
1 parent 858853a commit f11008a
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions examples/sheets/sheets-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@ import { Parser } from 'https://deno.land/x/xmlparser@v0.2.0/mod.ts'

// Example of how to fetch and read a Google spreadsheet
// For this to work without authentication, you have to publish the spreadsheet to the web
// File > Publish to the web > publish entire document as a web page
const sheetId = '1coaXmKes8b3GUFDGbmb-Hj4bek1HOEI-WCmOF4tIHwI'
const sheetNumber = 1;
const url = `https://spreadsheets.google.com/feeds/cells/${sheetId}/${sheetNumber}/public/values` // defaults to XML format
// const url = `https://spreadsheets.google.com/feeds/cells/${sheetId}/${sheetNumber}/public/values?alt=json` // alternative to fetch as json
// File > Publish to the web > publish entire document as a csv file

const url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQT7KW2U7o4zh2Sh5qpTl42OQzbKZG-glDd3rVcXhvQ1MtXTQiitvGEV6xvkfrXAn1dYADV1qLc2CEC/pub?output=csv'

// fetch the spreadsheet as XML
const response = await fetch(url)
const xml = await response.text()

// create an XML parser
const parser = new Parser({ /* options */ })
const root = parser.parse(xml)

// Go through data entries
const entries = root.getChild('feed')?.find(['entry'])
if (entries) {
for (const entry of entries) {
const cell = entry.getChild('cell')
console.log(cell?.getAttr('row'), ",", cell?.getAttr('col'))
console.log(cell?.getValue(''))
}
}
const csv = await response.text()
console.log(csv);

0 comments on commit f11008a

Please sign in to comment.