I'm trying to create a class in Javascript basically to do all the google sheet functions. And before writing any function I'm calling the authenticate API in the constructor, but the authentication is not being completed before calling the actual function. Here's the code in the class file,
const { google } = require("googleapis")
const authentication = require("./authentication");
class Sheets {
constructor() {
this.test = 'stackoverflow';
authentication.authenticate().then((auth)=>{
this.auth = auth;
});
}
async writeToSheet(spreadsheetId, range, data) {
if(typeof spreadsheetId != "undefined") {
console.log(this.test)
console.log('Got the spreadsheetId:' + spreadsheetId)
console.log('Got the auth:' + this.auth)
return;
}
var sheets = google.sheets('v4');
await sheets.spreadsheets.values.update({
auth: this.auth,
spreadsheetId: spreadsheetId,
range: range, //Change Sheet1 if your worksheet's name is something else
valueInputOption: "USER_ENTERED",
resource: {
values: data
}
}, (err, response) => {
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log("Data updated to the sheet successfully!");
}
});
}
}
module.exports = Sheets;
And this is how I'm calling the function,
let sheets = await new Sheets();
sheets.writeToSheet(spreadSheetId, range, data);
When I'm calling the writeToSheet function as above, It's always printing the auth as undefined only like this,
stackoverflow
Got the spreadsheetId:9ijWvEeWG7Oybv_TJi5AMkjl18dFHMFcBfSfkEG24-p7
Got the auth:undefined
So, how to call the authenticate function before calling any other function in the class and set the this.auth synchronously? Any help on this would be greatly appreciated.