It is often necessary to append multiple rows to a sheet in Google Sheets via Apps Script. Unfortunately, the built-in function Sheet.appendRow()
can append only one row at a time.
Of course, you could loop through the rows and append them one by one like this:
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Some Sheet")
const rows = [...]
rows.forEach(row => sheet.appendRow(row))
However, this approach has several drawbacks:
It is slow: each append operation needs time, which is manageable when appending 2-3 rows but becomes inefficient with 100 or more.
Each append will trigger formulas that use this range making it even slower and delaying next append operations. further slowing down the process and delaying subsequent append operations. In cases with abundant data/formulas, the delay can stretch into tens of seconds or even minutes.
There's no guarantee that the script will successfully append all rows. Each Apps Script operation, especially interacting with external resources, is prone to random failures. It's crucial to consider: if the script fails on line X, what will be the state of the data. Reducing the number of operations enhances reliability.
Better Solution: Custom Function
The following function implements a more efficient solution, which appends all rows in a single operation—drastically improving speed and reliability:
/**
* Appends rows to the given sheet with the specified values.
*
* @param {SpreadsheetApp.Sheet} sheet - The sheet to which rows will be appended.
* @param {Array<Array<*>>} rows - A 2D array containing the values to be appended. Each inner array represents a row, and each element within an inner array represents a cell value.
*/
const appendRows = (sheet, rows) => {
// Early exit if there are no values to append
if (rows.length === 0) return
// Get the range where the new rows should be appended:
// starting from the row following the last row with data, column 1,
// with dimensions matching the size of 'values'
sheet.getRange(
sheet.getLastRow() + 1,
1,
rows.length,
rows[0].length
).setValues(rows) // Set the values in the specified range
}
Utilizing this function is straightforward:
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Some Sheet")
const rows = [...]
appendRows(sheet, rows)
The preceding JSDoc annotation provides a helpful message and type hints (albeit without IDE validation) when utilizing the function.
Optimizing the process of appending multiple rows in Google Sheets is crucial for maintaining efficient and reliable script operations. While the built-in Sheet.appendRow()
function serves its purpose for appending single rows, the appendRows
function shared above vastly improves performance and reliability when dealing with multiple rows.
By reducing the number of operations and avoiding formula triggers on each append, this function significantly speeds up the process, especially in sheets with a large amount of data or formulas. This example demonstrates the importance of fine-tuning Google Apps Script operations to better handle larger datasets, ensuring your scripts run smoothly and reliably.
Top comments (0)