Laura Nguyen
Laura Nguyen

Reputation: 21

How to select all rows on Google Sheets using Google Apps Script?

I want to delete all the rows that I don't need on Google Sheets, my data has a table header.

Thanks!

Upvotes: 1

Views: 2035

Answers (1)

Tanaike
Tanaike

Reputation: 201643

I believe your goal as follows.

  • You want to select all rows using Google Apps Script.

In this case, how about the following sample script?

Sample script:

function muFunction() {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("C:C").activate();
}
  • When this script is run, the column "C" of "Sheet1" is selected.
  • For example, when "C:C" is modified to "C2:C", the cells "C2:C" are selected.

Note:

  • From I want to delete all the rows that I don't need on Google Sheets, my data has a table header., when you want to clear the rows except for the 1st header row, you can also use the following script. When the following script is run, the cells "C2:C" of "Sheet1" are cleared.

      function muFunction2() {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("C2:C").clearContent();
      }
    
  • From What's the code equivalent for Command + shift + down?, in this case, I thought that the following script is the result you expect? When you use the following script, please select a cell and run the script. By this, the rows of the data range after the selected cell are selected.

      function myFunction3() {
        const activeRange = SpreadsheetApp.getActiveRange();
        activeRange.offset(0, 0, activeRange.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() - activeRange.getRow() + 1).activate();
      }
    

References:

Upvotes: 2

Related Questions