{"id":15467,"date":"2023-08-02T09:00:00","date_gmt":"2023-08-02T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/08\/02\/exploring-getlastrowofcol-function-in-apps-script\/"},"modified":"2025-09-15T18:42:48","modified_gmt":"2025-09-15T09:42:48","slug":"exploring-getlastrowofcol-function-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/08\/02\/exploring-getlastrowofcol-function-in-apps-script\/","title":{"rendered":"Add parameters to getLastRowOfCol function"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a title=\"\" href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\">&#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<ol>\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<\/ol>\n<h2 id=\"summary\">Summary<\/h2>\n<p>In this blog post, we will explore the <code>getLastRowOfCol<\/code> function in Apps Script. This function is used to determine the last non-empty row in a specific column of a Google Sheets spreadsheet. <span style=\"color: #ff3366;\">In the previous post, the sheet and column were specified in the function, but we will change them into parameters so that they can be used in general cases.<\/span>We will analyze the code, explain how it works, and provide an example to demonstrate its usage.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function getLastRowOfCol(sheet, col) {\n  let lastRow = sheet.getMaxRows();\n  Logger.log(`${col}1:${col + lastRow}`)\n\n  let colValues = sheet.getRange(`${col}1:${col + lastRow}`).getValues();\n\n  for (; colValues[lastRow - 1] == '' &amp;&amp; lastRow &gt; 0; lastRow--) { }\n  return lastRow;\n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The <code>getLastRowOfCol<\/code> function takes two parameters: <code>sheet<\/code> and <code>col<\/code>. <span style=\"color: #ff3366;\">Here, sheet refers to the sheet itself designated by getSheetByName, not the sheet name.<\/span>It first retrieves the maximum number of rows in the sheet using the <code>getMaxRows<\/code> method.<\/p>\n<p>A log statement is used to display the range of cells being accessed in the sheet, which includes the specified column from the first row to the last row.<\/p>\n<p>The <code>getRange<\/code> and <code>getValues<\/code> methods are then used to retrieve the values of the cells within the specified range. These values are stored in the <code>colValues<\/code> variable.<\/p>\n<p>A for loop is used to iterate through the <code>colValues<\/code> array starting from the last row. The loop continues until it reaches a non-empty cell or until the row number becomes zero. The last non-empty row number is stored in the <code>lastRow<\/code> variable.<\/p>\n<p>Finally, the function returns the last non-empty row number.<\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s say we have a Google Sheets spreadsheet with the following data:<\/p>\n<table>\n<tbody>\n<tr>\n<th>Row 1<\/th>\n<th>Column A<\/th>\n<th>Column B<\/th>\n<th>Column C<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Row 2<\/td>\n<td style=\"text-align: center;\">Data 1<\/td>\n<td style=\"text-align: center;\">Data 2<\/td>\n<td style=\"text-align: center;\">Data 3<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Row 3<\/td>\n<td style=\"text-align: center;\">Data 4<\/td>\n<td style=\"text-align: center;\">Data 5<\/td>\n<td style=\"text-align: center;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Row 4<\/td>\n<td style=\"text-align: center;\">Data 6<\/td>\n<td style=\"text-align: center;\">Data 7<\/td>\n<td style=\"text-align: center;\">Data 8<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Row 5<\/td>\n<td style=\"text-align: center;\">Data 9<\/td>\n<td style=\"text-align: center;\"><\/td>\n<td style=\"text-align: center;\">Data 10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If we call the <code>getLastRowOfCol<\/code> function with the sheet and column parameters, it will return the last non-empty row number for that column. For example, <code>getLastRowOfCol(sheet, 'B')<\/code> will return 4, as the last non-empty row in column B is row 4.<\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p>Write a function that takes a Google Sheets sheet and a column as parameters. Inside the function, determine the last non-empty row in the specified column and return its row number. Use methods like <code>getMaxRows<\/code>, <code>getRange<\/code>, and <code>getValues<\/code> to achieve this. Include log statements to display the range of cells being accessed. Handle cases where the column may contain empty cells. Finally, test the function by calling it with appropriate parameters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, we will explore the getLastRowOfCol function in Apps Script. This function is used to determine the last non-empty row in a specific column of a Google Sheets spreadsheet. We will analyze the code, explain how it works, and provide an example to demonstrate its usage.<\/p>\n","protected":false},"author":1,"featured_media":15489,"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,534,7],"class_list":["post-15467","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-getlastrowofcolparam","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\/15467","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=15467"}],"version-history":[{"count":4,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15467\/revisions"}],"predecessor-version":[{"id":17232,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15467\/revisions\/17232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15489"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}