google sheets | test google data sheet | get worksheet by name | Search

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.

Run example

npm run import -- "add row data google sheet"

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;

What the code could have been:

// 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:

  1. Dependencies:

  2. addRow Function:

  3. 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!