2

When following the basic exmaple code from https://github.com/theoephraim/node-google-spreadsheet, it's not working.

for example:

const { GoogleSpreadsheet } = require('google-spreadsheet')
const creds = require('./gsheet-creds.json')

const doc = new GoogleSpreadsheet('1nkNX-HfxYY5qs_MwXTiFQIxEajibJlTTUPeOHCX7ZVc')

doc.useServiceAccountAuth({
    client_email: creds.client_email,
    private_key: creds.private_key,
})

await doc.loadInfo()
console.log(doc.title)

it shows error:

sz@air:/mnt/d/coding/wxBot-dorm-seller/googlesheet$ node gsheet-google-spreadsheet.js
/mnt/d/coding/wxBot-dorm-seller/googlesheet/gsheet-google-spreadsheet.js:34
await doc.loadInfo()
^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:1053:16)
    at Module._compile (internal/modules/cjs/loader.js:1101:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

If remove the 'await' before doc.loadInfo(), it shows error:

sz@air:/mnt/d/coding/wxBot-dorm-seller/googlesheet$ node gsheet-google-spreadsheet.js
gsheet@gsheet-yiouyou.iam.gserviceaccount.com
/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet@3.0.13@google-spreadsheet/lib/GoogleSpreadsheet.js:175
    if (!this._rawProperties) throw new Error('You must call `sheet.loadInfo()` before accessing this property');
                              ^

Error: You must call `sheet.loadInfo()` before accessing this property
    at GoogleSpreadsheet._ensureInfoLoaded (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet@3.0.13@google-spreadsheet/lib/GoogleSpreadsheet.js:175:37)
    at GoogleSpreadsheet._getProp (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet@3.0.13@google-spreadsheet/lib/GoogleSpreadsheet.js:192:10)
    at GoogleSpreadsheet.get title [as title] (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet@3.0.13@google-spreadsheet/lib/GoogleSpreadsheet.js:199:29)
    at Object.<anonymous> (/mnt/d/coding/wxBot-dorm-seller/googlesheet/gsheet-google-spreadsheet.js:35:17)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

I've tried many things and checked many examples from website and youtube. None of them is working for the latest google-spreadsheet. I haven't try anything older.

Appreciate any help here. Really want to get the google-spreadsheet.js working.

user1465767
  • 41
  • 1
  • 6

2 Answers2

0

You are using async + await wrong. You should use "await" only when calling a declared "async" function. See this answer for a detailed explanation of the use.

Aerials
  • 4,231
  • 1
  • 16
  • 20
0

Put the last 2 lines in an asynchronous function. I think that will help you.

For example:

const loadDoc = async (creds) => {
  await doc.loadInfo();
  let sheet = doc.sheetsByIndex[0];
  console.log(sheet.title);
};
loadDoc(creds);