I'm using Google Sheets API to copy a sheet, and I want to pass the ID of the new sheet into a variable.
I can successfully get the sheet ID within the callback function (using result.data.sheetId), but I'm struggling to use it outside of the function.
I'm using async / await syntax, and I have also experimented with utils.promisify, with no luck!
async function copySheet(jwt, apiKey, spreadsheetId, range) {
const sheets = google.sheets({ version: 'v4' });
let newSheetId = await sheets.spreadsheets.sheets.copyTo({
"spreadsheetId": spreadsheetId,
"sheetId": 0,
"auth": jwt,
"key": apiKey,
"resource": {
"destinationSpreadsheetId": spreadsheetId,
}
}, function (err, result) {
if (err) {
throw err;
}
console.log(result.data.sheetId);
return result.data.sheetId;
});
console.log(newSheetId);
}
The first console.log shows the number I want, but the second logs undefined.
Do you have any suggestions to allow me to use the sheetId outside of the callback function?