Office-js-docs-pr: Using variables in range

Created on 26 Mar 2020  Â·  3Comments  Â·  Source: OfficeDev/office-js-docs-pr

Its a common requirement to use variables in selected range. Is it possible and if yes, can you give an example for

  • rows only (say var range = sheet.getRange(rowFirst:rowLast); [ Dont know if this works though)
  • columns only (say var range = sheet.getRange(colFirst:colLast);
  • cells only (say var range = sheet.getRange("A"+firstRow +":" + "D" + :rowLast);
    All these are wrong examples but you get the idea. Is it possible?

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Excel programming question

Most helpful comment

Hi @PramodKumarYadav,

The getRange method takes in a string representing a cell range in the worksheet, such as "A1:D4", "A:D" for just the columns, or "1:4" for just the rows. As long as your variables map to Excel rows or columns, you can use string concatenation to build the argument. You may need to use range.columnIndex, range.rowIndex, and range.getCell to translate to and from zero-based numbers into the letter-based columns.

I'd recommend exploring these APIs by using Script Lab . There are plenty of samples to look at, plus it lets you use the Excel APIs without having to host a service.

In the future, I'd recommend raising programming questions such as this on Stack Overflow. That way, other members of the community can learn from your questions. Be sure to tag your Stack Overflow question with "office-js".

Because this question is more about programming than the specific APIs, I'm going to close the issue. Please let me know if you have any other questions.

All 3 comments

Hi @PramodKumarYadav,

The getRange method takes in a string representing a cell range in the worksheet, such as "A1:D4", "A:D" for just the columns, or "1:4" for just the rows. As long as your variables map to Excel rows or columns, you can use string concatenation to build the argument. You may need to use range.columnIndex, range.rowIndex, and range.getCell to translate to and from zero-based numbers into the letter-based columns.

I'd recommend exploring these APIs by using Script Lab . There are plenty of samples to look at, plus it lets you use the Excel APIs without having to host a service.

In the future, I'd recommend raising programming questions such as this on Stack Overflow. That way, other members of the community can learn from your questions. Be sure to tag your Stack Overflow question with "office-js".

Because this question is more about programming than the specific APIs, I'm going to close the issue. Please let me know if you have any other questions.

This helped Alex. I tried again with your explanation and it worked like a charm. Thank you!

Just to add an example, if someone else stumbles on this page:
const sheet = context.workbook.worksheets.getActiveWorksheet();
var firstrow = 1
var lastRow = 6
var range = sheet.getRange(firstrow + ":" + lastRow)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beyphy picture beyphy  Â·  4Comments

sameera picture sameera  Â·  5Comments

Scheel picture Scheel  Â·  4Comments

charliereese picture charliereese  Â·  5Comments

hyoshioka0128 picture hyoshioka0128  Â·  6Comments