Deleting a Label (First Element 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
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, retrieve data, remove the first row (which usually contains the column labels), and log the remaining data.
Code
1 2 3 4 5 6 7 8 |
function deleteLabel() { const SS = SpreadsheetApp.getActiveSpreadsheet() const T_SHEET6 = SS.getSheetByName('시트6') const data = T_SHEET6.getDataRange().getValues() let dataSlice = data.slice(1) Logger.log(dataSlice) } |
Code Explanation
The code defines a function named deleteLabel
. It starts by getting the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet()
. Then, it retrieves a specific sheet named ‘시트6’ using SS.getSheetByName('시트6')
.
The getDataRange()
method is used to get the range of data in the sheet. The getValues()
method is called on the data range to retrieve the values as a two-dimensional array.
Next, the slice()
method is used to remove the first row (which contains the labels) from the data array. The modified data is stored in the dataSlice
variable.
Finally, the remaining data (without the labels) is logged to the Logger using Logger.log(dataSlice)
.
Example
Let’s say we have a Google Spreadsheet with a sheet named ‘시트6’ 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 |
When the deleteLabel
function is executed, it will log the following remaining data:
Data 1 | Data 2 | Data 3 |
Data 4 | Data 5 | Data 6 |
AI Prompt
Write a function that deletes the labels in a specific sheet of a Google Spreadsheet. Retrieve the data range of the sheet, remove the first row (labels) using slice method, and log the remaining data.