Close

Accessing Goog sheets from javascript

A project log for Synchronize a PDF with a spreadsheset

PDFMerge

lion-mclionheadlion mclionhead 10/15/2023 at 01:260 Comments

ChatGPT was pretty useless at this.  Accessing goog sheet data begins with the goog cloud console,

https://console.cloud.google.com

A nest of thousands of acquisitions of things that were big for just long enough to make someone some money & then went nowhere.  Lions got oauth2 working in 2018 for AWS.  It seemed a lot simpler.  Reading goog sheet data from curl requires the browser to load a consent page with a URL

https://accounts.google.com/o/oauth2/auth?client_id=316481588438-4tqet7doqis04q8v4kfga3meb96ibstk.apps.googleusercontent.com&redirect_uri=http://localhost&scope=https://www.googleapis.com/auth/spreadsheets.readonly&response_type=code

https://youtube.com/watch?v=qwqJcyLQSSQ

There's a vijeo on creating several of the bits required to generate the consent URL, manely the client_id.  The consent URL is constructed from the auth_uri, client_id, redirect_uri, scope URI

https://youtube.com/watch?v=hBC_tVJIx5w

There's another vijeo showing how to get the access token with the client_id.

The app needs a scope URI for reading a spreadsheet: https://www.googleapis.com/auth/spreadsheets.readonly

The redirect_uri is the page the browser should load upon success.  It should point back to the javascript file.   It puts an authorization code in the URI which lasts only a few minutes.  The javascript program has to use the authorizaton code to immediately get an access token + refresh token.  This is where the goog starts throwing origin errors.  The token request has to be indirected through a server using curl.

The access token can then be used to read the spreadsheet for 1 hour.  This too must be done on the server.  The curl request to read a spreadsheet is

curl -X GET \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json"  \
"https://sheets.googleapis.com/v4/spreadsheets/$SPREADSHEET_ID/values/Sheet1!A1"

Helas, the Sheet1 identifier is not contained in a copied cell link.  For now, the program is going to be limited to Sheet1

Then it gives a JSON result

{

  "range": "Sheet1!A1",
  "majorDimension": "ROWS",
  "values": [
    [
       "CELL CONTENTS"
    ]
  ]
}

The internet says the app has to use a refresh token to renew the access token every hour.  There needs to be a complete infrastructure for managing the refresh tokens, access tokens, consent page in a cookie.  Sadly, the goog doesn't provide a refresh token for the lion kingdom.  Despite many random search results, the latest dance seems to be to reload the consent screen when the access token expires.  The goog then either shows the consent screen or provides another access token.

After much coersion, the lion kingdom managed to draw a spreadsheet cell on a PDF with the PDF copying changes from the spreadsheet.

--------------------------------------------------------------------------

Not counting the difficulty of accessing a goog sheet, there's an overall explosion of complexity.  It needs to handle the access tokens as transparently as possible by handling errors & transparently restoring state when the consent page exits.   It needs functions to shift cells, undo, redo, select a different font size for every cell, enter arbitrary text.  It's going to need a faster way than running curl for every cell.

Discussions