The script would look like this :
const { google } = require('googleapis');
const sheets = google.sheets('v4');
// Load the credentials from the downloaded JSON file
const credentials = require('./path-to-your-credentials.json');
const auth = new google.auth.GoogleAuth({
credentials: credentials,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
async function addNewRow() {
const authClient = await auth.getClient();
// Specify the spreadsheet ID and range where you want to add the new row
const spreadsheetId = 'your-spreadsheet-id';
const range = 'Sheet1!A:A'; // Adjust the range as needed
// Create the new row data
const newRowData = {
values: [['New Column 1 Data', 'New Column 2 Data', '...']],
};
try {
// Append the new row data to the specified range
const response = await sheets.spreadsheets.values.append({
auth: authClient,
spreadsheetId: spreadsheetId,
range: range,
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: newRowData,
});
console.log('New row added:', response.data);
} catch (err) {
console.error('Error adding new row:', err.message);
}
}
addNewRow();