Returns the column number of the last column that has content.
class SpreadsheetApp method .getLastColumn()
SpreadsheetApp is the Apps Script service.
.getLastColumn() is the method name you will see after a dot in your code.
What it does
Why it's used
- Detect how wide a table is before looping across columns.
- Know where to write the next new field without hardcoding column letters.
Common errors
- Forgetting parentheses on getLastColumn. Write SpreadsheetApp.getLastColumn(), not SpreadsheetApp.getLastColumn or getLastColumn by itself.
- Skipping dots between chained calls. Each step needs a dot, like SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data').getLastColumn().
- Adding spaces where they do not belong. Write SpreadsheetApp.getLastColumn(), not SpreadsheetApp .getLastColumn(), SpreadsheetApp.getLastColumn (), or SpreadsheetApp . getLastColumn().
- Wrong capitalization. Use SpreadsheetApp and getLastColumn, not spreadsheetapp or getlastcolumn.
Explore
Related methods
-
.getLastRow()
Returns the row number of the last row that has content.
-
.getColumn()
Returns the starting column index of a range.
-
.getMaxColumns()
Returns the total number of columns the sheet can hold.
-
.getNumColumns()
Returns how many columns are in a range.
-
.getRow()
Returns the starting row index of a range.
More Apps Script
Better Sheets tutorials
No tagged tutorials yet. Fetch Apps Script from the harvest table and this page fills automatically.
Related blog posts
Written guides about SpreadsheetApp.getLastColumn().
4 Ways to Resize Columns, Change the Column Width in a Google Sheet
Adjusting column width is crucial for enhancing the readability and functionality of spreadsheets. Properly sized columns ensure that all data is visible without unnecessary scrolling or cutting off information, which can lead to errors or misinterpretations.
Read post →How to reference a different Google Spreadsheet in Apps Script?
Transferring Data Between Cells in Different Spreadsheets
Read post →Google Sheets Locales. What are they?
Hello, Better Sheet members and non-Better Sheet members! This tutorial is about every locale listed in Google Sheets. If you live in a country that is not well represented with blog posts or someone makes a blog post about a Google Sheets issue, they create a Google Sheet formula and then you copy and paste it into your sheet and it has the wrong delimiter. How do you resolve that? I'm going to show you how you can fix that and how you can know if there's a problem. Locales. What are the...
Read post →