Append a row of data to the end of the current sheet

Append a row of data to the end of the current sheet

Instructions:

  • Follow the general script setup instructions.
  • Paste this code into the script editor.
  • Save and run the appendRowToSheet function.

Explanation: This script appends a new row to the bottom of the active sheet with the data provided in the rowData array.

function appendMyRow(){

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(‘Sheet1’);

  const rowData = [‘New’,’Laurence’,’Svekis’];

  sheet.appendRow(rowData);

  //Logger.log(sheet.getName());

}

Google Apps Script code snippet appends a new row of data to a Google Sheets spreadsheet. Let’s break down the code:

  • const rowData = [‘New’,’Laurence’,’Svekis’];: This line declares a constant variable named rowData and assigns it an array containing three elements: ‘New’, ‘Laurence’, and ‘Svekis’. Each element represents a value that will be added to a cell in the new row.
  • sheet.appendRow(rowData);: This line appends a new row of data to the Google Sheets spreadsheet referenced by the sheet variable. It uses the appendRow() method, which is a built-in function in Google Apps Script for adding a new row to the end of the specified sheet.

Here’s what each part of the code does:

  • The rowData array contains the values that you want to insert into the new row. In this case, you have three values: ‘New’, ‘Laurence’, and ‘Svekis’.
  • The sheet variable should be previously defined to reference the specific sheet within the Google Sheets spreadsheet where you want to append the new row. For example, you can obtain the sheet using getSheetByName() or another method based on your needs.
  • The appendRow(rowData) method takes the rowData array as its argument and adds a new row to the specified sheet with the values from the rowData array. Each value in the array is placed in a separate cell in the new row, starting from the first column.

When you execute this code, it will add a new row to the specified sheet with the values ‘New’, ‘Laurence’, and ‘Svekis’ in the respective columns.

This is a common way to programmatically insert data into a Google Sheets spreadsheet using Google Apps Script, and it’s particularly useful for automating data entry tasks or updating spreadsheet content dynamically.