Changing Column Order and Adding Text in 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
This blog post will guide you through the process of changing the column order and adding text in Apps Script. Using the provided code, you will be able to rearrange columns in a Google Spreadsheet and append additional text to a specific column.
Code
1 2 3 4 5 6 7 8 |
function colChangeAddText() { const SS = SpreadsheetApp.getActiveSpreadsheet() const T_SHEET6 = SS.getSheetByName('시트6') const data = T_SHEET6.getDataRange().getValues() let dataMap = data.map(item => [item[1], item[0], item[2] + '살']) Logger.log(dataMap) } |
Code Explanation
The code defines a function named colChangeAddText
. It accesses the active spreadsheet and retrieves a specific sheet named ‘시트6’ using SpreadsheetApp.getActiveSpreadsheet()
and getSheetByName()
methods.
The getDataRange()
method is used to retrieve the range of data from the sheet, and the getValues()
method is used to get the actual data as a two-dimensional array.
The data.map()
function is used to iterate over each row of the data array and rearrange the column order. In this example, the second column is moved to the first position, the first column is moved to the second position, and the third column has additional text appended to its value.
The modified data is stored in the dataMap
variable, and the result is logged using Logger.log()
.
Example
Let’s say we have a spreadsheet with three columns: A, B, and C. The original data looks like this:
Country | Name | Age |
---|---|---|
USA | John | 25 |
Canada | Jane | 25 |
After running the colChangeAddText()
function, the column order will be changed, and the additional text ‘살’ will be appended to the values in column C. The modified data will look like this:
Name | Country | Age |
---|---|---|
USA | John | 25살 |
Canada | Jane | 25살 |
AI Prompt
Rearrange the columns in a Google Spreadsheet and append the text ‘살’ to the values in the third column.
Get all the values from Google Sheet with Apps script, change the position of the columns using map, and add text to one column.