Delete columns

1,030 views
Skip to first unread message

John

unread,
Jun 29, 2022, 7:21:59 AM6/29/22
to Google Apps Script Community
Hello Can some assist with creating an Apps script to find the last column in a sheet which has data in row 1 and to delete the rest of the columns to the right.

Or alternatively add the last column number in the script ie 26 for column "Z" and delete everything to the right of column 26(Z)

Any help is much appreciated

Jonathan Butler

unread,
Jun 29, 2022, 8:01:04 AM6/29/22
to google-apps-sc...@googlegroups.com
Hi,

Do you have an example sheet?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/630c97c1-d561-4cea-aa7b-8bf8d045e822n%40googlegroups.com.

John

unread,
Jun 29, 2022, 8:21:24 AM6/29/22
to Google Apps Script Community
Sorry no example sheet that I can share as of yet. 
Is something not clear in my description?

Jonathan Butler

unread,
Jun 29, 2022, 8:48:52 AM6/29/22
to google-apps-sc...@googlegroups.com
"  find the last column in a sheet which has data in row 1 and to delete the rest of the columns to the right.  " Is somewhat unclear. You say you want to delete columns to the right, it sounds like you want to remove all the columns for all the rows to the right of the selected column. Do you want to delete all the data in the cells instead? 

John

unread,
Jun 29, 2022, 2:29:49 PM6/29/22
to Google Apps Script Community
So, a script will look across row 1 to find the last column with data i.e. cell Z1 has a date but AA1 is empty.  
I then want to delete all columns to the right of the last column with data "Z1" to the end of columns
I am thinking that the last column can identified either by entering the last column number in the script (which would the same on all sheets) or checking for data in row 1. 
There can be numerous columns after the last data column which are just blank and I want to remove them all so that google sheets only shows columns with data in
 them.

Stephen Barker

unread,
Jun 30, 2022, 8:16:30 AM6/30/22
to Google Apps Script Community
If you know Z1 has data and AA1 does not you can just use the normal sheet.getLastColumn()(commented out in code) but if AA2 has data and you want to still delete it that's a pain on dynamic data ranges but, if like you said you know it's always going to be Z1 you want to keep just update the col data variable at the top:

const COL_TO_DELETE_AFTER = 26 //a=1,j=10,z=26...

//Add to Menu
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Sheet Tools')
      .addItem('Remove Empty Columns', 'removeEmptyColumns')
      .addSeparator()
      .addItem('Remove Empty Rows', 'removeEmptyRows')
      .addToUi();
}

//Remove All Empty Columns in the Entire Workbook
function removeEmptyColumns() {
  var ss = SpreadsheetApp.getActive();
  var allsheets = ss.getSheets();
  for (var s in allsheets){
    var sheet=allsheets[s]
    var maxColumns = sheet.getMaxColumns(); 
    //var lastColumn = sheet.getLastColumn();
    var lastColumn = COL_TO_DELETE_AFTER;
    if (maxColumns-lastColumn != 0){
      sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
    }
  }
}

//Remove All Empty Rows in the Entire Workbook
function removeEmptyRows() {
  var ss = SpreadsheetApp.getActive();
  var allsheets = ss.getSheets();
  for (var s in allsheets){
    var sheet=allsheets[s]
    var maxRows = sheet.getMaxRows(); 
    var lastRow = sheet.getLastRow();
    if (maxRows-lastRow != 0){
      sheet.deleteRows(lastRow+1, maxRows-lastRow);
    }
  }
}

 

Message has been deleted

John

unread,
Jul 2, 2022, 11:29:32 AM7/2/22
to Google Apps Script Community
Thanks for the support

Using the  sheet.getLastColumn() works as I need and clears any columns after Z1.

much appreciated

Antony Lo

unread,
Jul 28, 2022, 1:54:26 AM7/28/22
to Google Apps Script Community
This is great - thank you for this script!
Reply all
Reply to author
Forward
0 new messages