24

The best workaround I have found so far is:

worksheet_name!$A$1:$YY

but ideally I would like to be able to simply write, e.g.:

worksheet_name!

So, does anyone know: is there a syntax for specifying a whole sheet as a range?

migrated from superuser.com May 12 '13 at 17:05

This question came from our site for computer enthusiasts and power users.

10

I've created a small Google Apps Script (GAS) snippet, to do the work for you.

Code

function sheetRange(targetName,int) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var asName = ss.getActiveSheet().getSheetName();
  var tgSheet = ss.getSheetByName(targetName);
  var output;

  if(targetName == asName) {
    output = "Error: target sheet is active sheet !!";
  } else {
    switch(int) {
      case 1: 
        output = tgSheet.getDataRange().getValues();
        break;
      case 2:
        output = tgSheet.getSheetValues(1, 1, tgSheet.getMaxRows(),  
          tgSheet.getMaxColumns());
        break;
      default:
        output = "Choose int to be 1 or 2 !!";
    }
  }
  return output;
}

In the spreadsheet menu, select Tools>Script editor, and add the code. Make sure to press the bug button:
enter image description here

Usage

=sheetRange("sheetName",int)

Use the int option as explained under remarks.

Example

I've created an example file you you: Sheet as Range

Remarks

In this case, there are two ways to determine a range in a spreadsheet via GAS:

  1. int=1; Via the getDataRange method. This will retrieve a range, in which the last column is the one that has data in it. The same accounts for the number of rows. This is usually the most straightforward route. See second sheet in example file.
  2. int=2; Via the getSheetValues method. This will retrieve a "WYSIWYG" range. See third sheet in example file.
  3. Refresh rates of these types of custom function aren't immediate, so please be patient. It may take several hours for data to be refreshed.

References

  • Why would it take several hours for this function to show refreshed data? I thought the whole point of using a function like this would be to always have the accurate range? – ClearCloud8 yesterday
5

You can use A:Z or A:AB or A:XX (with XX being the last column of your page):

Screenshot of using this to apply conditional formatting to rows based on a single cell's value

  • 1
    Ist dies: "Denk Mal Neu"? How does this answer the question? – Jacob Jan Tuinstra Sep 18 '15 at 10:10
  • 1
    Everybody here knows how to specify a range. This wasn't the question. – user79865 Sep 18 '15 at 11:22
3

Google Sheets doesn't have a range syntax for whole sheet range.

The following formula will return the range address of the whole sheet named Sheet1

="Sheet1!"&ADDRESS(1,1,,TRUE)&":"&ADDRESS(ROWS(Sheet1!A:A),COLUMNS(Sheet1!1:1),,TRUE)

In order to use it as reference, put it inside of INDIRECT. The following formula will return an array of all the values in Sheet1.

=ArrayFormula(
  INDIRECT(
    "Sheet1!"
    &ADDRESS(1,1,,TRUE)
    &":"
    &ADDRESS(ROWS(Sheet1!A:A),COLUMNS(Sheet1!1:1),,TRUE)
    ,
    TRUE
  )
)
1

I don't know a specific one, but I think you can use some formulae if you don't know the number of rows/columns:

indirect("Sheet!1:"&countif(Sheet!A:A,"<>@")+countif(Sheet!A:A,"=@"))

Here, Sheet is your sheet name, and @ is an arbitrary string. If that sheet you're selecting has @ in one cell, it won't work. You can replace it by another character if ever the sheet contains such a cell.

The COUNITF here counts the number of rows in column A:A not containing @, which should be all if there are no cells containing it, and INDIRECT transforms Sheet!1:### (where ### is the number of rows) into an actual selected range.

  • I appreciate the effort, but as far as I can tell, your suggestion yields no advantages over the workaround I mentioned in my question, but has the disadvantages of being more verbose and requiring the user to input a character that won't ever be present in the sheet. – sampablokuper May 28 '13 at 16:10
  • @sampablokuper About it being verbose, I don't think I can find a solution for that, as for the second part, I overlooked something which I have now added to my answer. Also, as far as I can see, the advantage it has over your current workaround is that you don't need to know how many rows or columns the sheet has. But yes, that's as far as it gets :( Perhaps one can make up a function from a VBA script. – Jerry May 28 '13 at 16:54
  • Thanks, but my current workaround does not require one to know how many rows the sheet has, nor how many columns it has as long as it has <676 columns. – sampablokuper May 29 '13 at 19:56
1

To specify the entire sheet as a range, please use:

worksheet_name!A1:ZZ

you can try it here: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear

or if you use Java (to clear everything in Sheet1):

Sheets service = getSheetsService(credential);
service.spreadsheets().values().clear(REPORT_WARNINGS_LATAM_FILEID, "Sheet1!A1:ZZ", new ClearValuesRequest()).execute();

or if you want just to use it in a formula (for example: SUM) on another sheet (for example: Sheet2) you can use following reference:

=SUM(Sheet1!A1:ZZ)

^ this will sum all existing cells on Sheet1 and put the value in a cell on Sheet2, you can find the example here: https://docs.google.com/spreadsheets/d/1rP3YCl3ErlYjlYGT_Q-xFvkVr7yKw6WQfHklbohL1NM/edit?usp=sharing. In this example we have 3 columns and 5 rows on Sheet1, so this formula =SUM(Sheet1!A1:ZZ) selects all of them. You can add rows or columns on Sheet1 and this solution will still select all cells.

P.S.: if you down vote my answer - please explain why.

1

This works for me:

var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var rg = sh.getName() +"!"+ sh.getDataRange().getA1Notation();

Used in a bound script, combine the sheetname with a "!" and then get the A1 notation of the data range on the chosen sheet

0

I was able to select the entire sheet by naming the range as the sheet without the exclamation mark.

I had a tab named data. Instead of using data! or data!A1:ZZ.

  • When we assign a name to a named range, a range reference should be specified. What reference range do you used? – Rubén Jan 4 '18 at 3:31
0

The best answers have already been given for those who are operating within appscript, but if anyone out there is working within Google Sheets proper, then here's an approach that may fit your needs:

"worksheet_name!1:" & ROWS(worksheet_name!A:A)

Explanation: * You're essentially specifying the range via a string that's concatenated with the number of rows in worksheet_name!

Some advantages of this approach: * It's 'dynamic'... if you add rows to the bottom, the range will adjust accordingly * Lightweight - you can slap this into an importrange/query function

Disadvantages: * Haven't tried it in all cases so might only work for specific use cases * I personally have a preference for making things dynamic / clean so a custom function of sorts would be a good middle ground between appscript and this lightweight approach

-2

Assuming you know the number of rows, you can name the entire sheet as "worksheet_name":

STEPS:

  1. Select the entire worksheet
  2. Click 'Data' -> 'Named and Protected Ranges'
  3. Type "worksheet_name" to name the selection & click Done.

Now every time you use "worksheet_name" in a function, it will reference the entire worksheet.

  • 4
    Your method will result in a "snapshot" range. Adding both columns or rows will not change the named range !! – Jacob Jan Tuinstra Aug 12 '13 at 20:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.