user2758054
user2758054

Reputation: 41

Google Script for deleting blank or unused columns

I have searched and searched but can't find anything that works. I am trying to eliminate any empty columns that remain in the spreadsheet when creating a Google Form. It would be helpful because I have a formula for the sum of a string comparison in the sheet, but I don't want it to include comparisons of empty cells. (i.e. I compare the entries in from H:X in every individual row to H2:X2 and have it count how many are the same.) I know I could adjust the formula, but I am looking to build a template for fellow colleagues so they don't have to worry about altering anything. If I could get the empty columns at the end to disappear automatically, I could just change the sum formula to extend all the way to column CZ (just to be sure it goes far enough), without it calculating blanks in its comparison.

Any help would be great! Thanks!

Upvotes: 3

Views: 5736

Answers (3)

Trey
Trey

Reputation: 31

i use these scripts in the script editor of my google sheets for cleaning them up. stumbled across them somewhere... perhaps they will help.

//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();
    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);
    }
  }
}

Upvotes: 3

Raul Marquez
Raul Marquez

Reputation: 1116

Using Arrayformula is better suited for this though it does the same thing.

Example: =ARRAYFORMULA('Form Responses'!A1:EA)

Importrange has a noticeable (and annoying) delay as this what you would use if you wanted to reference sheets from other spreadsheet files. Arrayformula references sheets within the same file, no delays.

Upvotes: 0

Tim
Tim

Reputation: 766

This is something you will have to get used to when using Google forms. Data is never really erased if you physically delete rows/columns, they will just reappear at some unknown time in the future. It's a pretty buggy and unreliable setup.

My advice is to keep your formulas and data completely separate. You do this by copying the form responses to another sheet (using code or otherwise). That sheet allows you to control the data being fed (or NOT fed) to the rest of your system. I would also go 1 step further and use formulas like

ImportRange(SheetSource!A:A) 

in a 3rd sheet to allow you to control the order of the columns. From there you can add your formulas to act on the data being fed through in relative safety.

However, I had a situation not long ago, where the form and the sheet randomly lost their linkage, and it was not possible to restore it. I stayed up until 6am trying to fix the issue, however if I did not have the data copied to the separate sheet (as described above) the catastrophe would have been complete.

Luckily this appeared to have been a random glitch, because everything went back to normal at about 10am, but put simply, your data is not truly safe with Google Forms.

Upvotes: 0

Related Questions