Here is a sample code to utilize spreadsheets.values.batchUpdate
with NodeJS and the 'googleapis' module in Google Cloud Functions:
npm install googleapis
Then, you need to authenticate with the Google Sheets API by creating credentials and saving the JSON file.
In your code, import the 'googleapis' module and create a client instance:
const {google} = require('googleapis');
const auth = new google.auth.GoogleAuth({
keyFile: 'YOUR_CREDENTIALS.json',
scopes: [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
});
const sheetsApi = google.sheets({version: 'v4', auth});
spreadsheets.values.batchUpdate
method to update the values in a sheet:async function batchUpdateValues(spreadsheetId, range, values) {
const request = {
spreadsheetId,
resource: {
valueInputOption: 'USER_ENTERED',
data: [
{
range,
values
}
]
}
};
try {
const response = await sheetsApi.spreadsheets.values.batchUpdate(request);
console.log(`Updated ${response.data.totalUpdatedCells} cells.`);
} catch (err) {
console.error(err);
}
}
batchUpdateValues
function with the spreadsheet ID, range, and values you want to update:const spreadsheetId = 'YOUR_SPREADSHEET_ID';
const range = 'Sheet1!A1:B2';
const values = [
['New value 1', 'New value 2'],
['New value 3', 'New value 4']
];
batchUpdateValues(spreadsheetId, range, values);
This will update the cells in the specified range with the new values.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-03-25 11:00:00 +0000
Seen: 11 times
Last updated: Apr 21 '22
How to resolve the error "Cannot find module" when deploying AWS Amplify?
What does the error message "Module not found: Error: Can't resolve JSON" mean?
What is the process for transforming a date into the desired format using the dayjs module?
How can data be transmitted to the ApGenCode widget within the Ap Page Builder module in Prestashop?
Is it not possible to import the nltk module in Jupyter notebook?