Skip to content

Commit

Permalink
feature google spread sheet api
Browse files Browse the repository at this point in the history
  • Loading branch information
tsengyushiang authored Apr 23, 2024
1 parent 47a278f commit a275573
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* [Deploy](./deploy)
* [Docker](./docker)
* [Git](./git/)
* [Google App Script](./google-apps-script/)
* [Google](./google/)
* [Backend](./backend/)
* [Frontend](./frontend/)
* [LaTex](./Latex/)
Expand Down
4 changes: 4 additions & 0 deletions google/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Google

* [google-apps-script](./google-appa-script.md)
* [spread-sheet-api](./spread-sheet-api.md)
File renamed without changes.
55 changes: 55 additions & 0 deletions google/spread-sheet-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Spread Sheet API

## Sample Data

| name | dept | lunchTime | salary | hireDate | age | isSenior | seniorityStartTime |
|-------|-----------|-----------|-------:|------------|-----|----------|---------------------|
| John | Eng | 12:00:00 | 1000 | 2005-03-19 | 35 | TRUE | 2007-12-02 15:56:00 |
| Dave | Eng | 12:00:00 | 500 | 2006-04-19 | 27 | FALSE | null |
| Sally | Eng | 13:00:00 | 600 | 2005-10-10 | 30 | FALSE | null |
| Ben | Sales | 12:00:00 | 400 | 2002-10-10 | 32 | TRUE | 2005-03-09 12:30:00 |
| Dana | Sales | 12:00:00 | 350 | 2004-09-08 | 25 | FALSE | null |
| Mike | Marketing | 13:00:00 | 800 | 2005-01-10 | 24 | TRUE | 2007-12-30 14:40:00 |

## Data Parser

- Converts text from api response to array.

```javascript
const mapText2Array = (sheets) => {
if (!sheets) return null;

const sheetsPattern =
/google\.visualization\.Query\.setResponse\(([\s\S\w]+)\)/;
const sheetJson = sheets.match(sheetsPattern);

if (!sheetJson || !sheetJson[1]) {
console.log("Failed to match Google Sheets response pattern");
return null;
}

try {
const sheetData = JSON.parse(sheetJson[1]).table.rows;
const sheetRows = sheetData.map((row) => row.c.map((col) => col.v))

return sheetRows;
} catch (err) {
console.log("Error parsing JSON from Google Sheets");
return null;
}
};
```

## APIs

- Get text from spread sheet with `GET https://docs.google.com/spreadsheets/d/${sheet_id}/gviz/tq?tqx=out:json`

```javascript
await fetch("https://docs.google.com/spreadsheets/d/12-XhOC8wCcLsKHYcoY-E3N-hKqPjw2xaZKUNlntod2s/gviz/tq?tqx=out:json").then(data=>data.text()).then(mapText2Array);
```

- Add [Query Langauge](https://developers.google.com/chart/interactive/docs/querylanguage ) with query stirng `tq=${encodeURIComponent("select *")}`

```javascript
await fetch(`https://docs.google.com/spreadsheets/d/12-XhOC8wCcLsKHYcoY-E3N-hKqPjw2xaZKUNlntod2s/gviz/tq?tq=${encodeURIComponent("where F > 30")}`).then(data=>data.text()).then(mapText2Array)
```

0 comments on commit a275573

Please sign in to comment.