The addRow
function adds a new row to a specified sheet in a Google Sheet by leveraging the updateRow
function from a local module. It takes the sheet URL, the new row data, and the sheet number as input.
npm run import -- "add row data google sheet"
var util = require('util');
var importer = require('../Core');
var updateRow = importer.import("update a row in google sheets");
function addRow(link, newRow, page) {
return updateRow(link, r => false, newRow, page)
}
module.exports = addRow;
// Import necessary modules
const { google } = require('googleapis');
const core = require('../Core');
const { updateRow } = core.import('update a row in google sheets');
/**
* Adds a new row to a Google Sheet.
*
* @param {string} link - The link to the Google Sheet.
* @param {object} newRow - The data to be inserted into the new row.
* @param {string} page - The page number where the row will be added.
* @param {string} sheetName - The name of the sheet in the Google Sheet.
* @param {function} callback - An optional callback function.
* @returns {Promise} A promise that resolves to an object containing the response from the API.
*/
async function addRow({
link,
newRow,
page,
sheetName = 'Sheet1', // Default sheet name
callback,
}) {
// Check if all necessary parameters are provided
if (!link ||!newRow ||!page) {
throw new Error('Missing required parameters');
}
// Update the row and return the response
return updateRow(link, r => false, newRow, page, sheetName, callback);
}
// Export the function
module.exports = addRow;
This code defines a function addRow
that adds a new row to a specific sheet in a Google Sheet.
Here's a breakdown:
Dependencies:
util
for utility functions (though it's not used in this snippet).updateRow
from a local module Core
, which likely handles updating rows in Google Sheets.addRow
Function:
link
), a new row (newRow
) as an object, and a page number (page
) as input.updateRow
with the following arguments:
link
: The Google Sheet URL.r => false
: A function that always returns false
, indicating that no existing row should be updated.newRow
: The new row data to be added.page
: The page number (likely referring to a specific sheet within the spreadsheet).Export: The addRow
function is exported, allowing other parts of the application to use it to add new rows to Google Sheets.
Let me know if you'd like a deeper dive into any specific part of the code!