{"id":15639,"date":"2023-09-01T09:00:00","date_gmt":"2023-09-01T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/09\/01\/changing-columns-in-apps-script\/"},"modified":"2025-09-15T17:31:05","modified_gmt":"2025-09-15T08:31:05","slug":"changing-columns-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/09\/01\/changing-columns-in-apps-script\/","title":{"rendered":"Changing column order using map in Apps script"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\" title=\"\">&#8220;Google Apps Script 101: Building Work Automation For Free&#8221;<\/a>. Added a <s>strikethrough<\/s> when editing what AI has written, and added <span style=\"color: #ff3366;\">color<\/span> when edited by author<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#code\">Code<\/a><\/li>\n<li><a href=\"#explanation\">Code Explanation<\/a><\/li>\n<li><a href=\"#example\">Example<\/a><\/li>\n<li><a href=\"#ai-prompt\">AI Prompt<\/a><\/li>\n<\/ul>\n<h2 id=\"summary\">Summary<\/h2>\n<p>This blog post will walk you through the process of changing columns in Apps Script. We will learn how to extract data from a specific sheet and rearrange the columns in a desired order.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function colChange() {\n  const SS = SpreadsheetApp.getActiveSpreadsheet();\n  const T_SHEET6 = SS.getSheetByName('Sheet6');\n\n  const data = T_SHEET6.getDataRange().getValues();\n  let dataMap = data.map(item => [item[1], item[0], item[2]]);\n  Logger.log(dataMap);\n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The code defines a function named <code>colChange<\/code>. Inside the function, it gets the active spreadsheet and assigns it to the <code>SS<\/code> variable. Then, it retrieves the sheet named &#8216;Sheet6&#8217; and assigns it to the <code>T_SHEET6<\/code> variable.<\/p>\n<p>The <code>getDataRange()<\/code> method is used to get the range of data in the sheet, and the <code>getValues()<\/code> method is called to retrieve all the values in that range. The data is stored in the <code>data<\/code> variable.<\/p>\n<p>The <code>map()<\/code> function is used to iterate through each item in the <code>data<\/code> array and create a new array <code>dataMap<\/code> with the columns rearranged. 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 remains unchanged.<\/p>\n<p>Finally, the <code>dataMap<\/code> array is logged to the Logger.<\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s say you have a spreadsheet with three columns: Name, Age, and Occupation. You want to rearrange the columns so that the Occupation column comes first, followed by the Name column, and then the Age column.<\/p>\n<p>By executing the <code>colChange<\/code> function, the columns will be rearranged as desired.<\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p>Create a function that changes the order of columns in a spreadsheet. The function should retrieve the data from a specific sheet and rearrange the columns in the desired order. Log the resulting data array to the Logger.<\/p>\n<p>For example, if the original columns are [&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;], and you want to rearrange them as [&#8216;C&#8217;, &#8216;A&#8217;, &#8216;B&#8217;], the resulting array should be logged as [[&#8216;C&#8217;, &#8216;A&#8217;, &#8216;B&#8217;]].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will walk you through the process of changing columns in Apps Script. We will learn how to extract data from a specific sheet and rearrange the columns in a desired order.<\/p>\n","protected":false},"author":1,"featured_media":15651,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[5,118],"tags":[512,69,542,7],"class_list":["post-15639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-map","tag-spreadsheet"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["ko","en"],"languages":{"ko":{"title":true,"content":true,"excerpt":true},"en":{"title":true,"content":true,"excerpt":true}}},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/comments?post=15639"}],"version-history":[{"count":3,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15639\/revisions"}],"predecessor-version":[{"id":17175,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15639\/revisions\/17175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15651"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}