|

Get sliced range of elements of 2d array with Apps Script

This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author

Table of Contents

  1. Summary
  2. Code
  3. Code Explanation
  4. Example
  5. AI Prompt

Summary

In this blog post, we will explore how to delete labels in Apps Script. We will learn how to access a specific sheet in a Google Spreadsheet, get the data range, slice the data to select a specific range of rows, and log the sliced data.

Code

Code Explanation

The code declares a function named getSlicedData. It retrieves the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet() and assigns it to the SS variable.

The code then uses SS.getSheetByName('Sheet6') to access a specific sheet named ‘Sheet6’ and assigns it to the T_SHEET6 variable.

The getDataRange() method is used to retrieve the data range of the sheet, and the getValues() method is used to get the values of the data range.

A portion of the data is sliced using the slice() method, specifying the start and end indexes of the desired range of rows. The sliced data is stored in the dataSlice variable.

In this case, the first number 1 represents index 1 of the array, including index 1. The second number 3 means index 3, but does not include index 3, but includes up to 2.

Finally, the sliced data is logged using Logger.log(dataSlice).

Example

Suppose you have a Google Spreadsheet with a sheet named ‘Sheet6’ that contains the following data:

Column A Column B Column C
Label 1 Label 2 Label 3
Data 1 Data 2 Data 3
Data 4 Data 5 Data 6
Data 7 Data 8 Data 9

If the above getSlicedData function is executed, the remaining data with labels removed will be written to the log as follows:

Data 1 Data 2 Data 3
Data 4 Data 5 Data 6

AI Prompt

Get a range of data from a specific sheet in Google Sheets, select a range of rows using slice method , and write the selected data to a log.

Similar Posts