question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I select a range of cells?

See original GitHub issue

Something like this would be nice:

worksheet.getRange('B1:E22')

but I don’t see anything like it in the readme.

Ultimately, I am trying to add a border around a range like so: image

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:15
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
kendallrothcommented, Dec 12, 2019

I’m fairly surprised that this hasn’t been a more commonly requested feature; is it something that we could see support for in the near future?

Here’s what I’ve come up with in the mean time, not quite fully error-proof (missing a few checks) but close enough:

/**
 * Select a range of cells
 * @param  {string}   startCell - Range start cell (top-left)
 * @param  {string}   endCell   - Range end cell (bottom-right)
 * @return {Object[]}           - Selected cells
 */
const selectRange = (sheet, startCell, endCell) => {
  const [endCellColumn, endRow] = endCell.split(":", 2);
  const [startCellColumn, startRow] = startCell.split(":", 2);

  let endColumn = sheet.getColumn(endCellColumn);
  let startColumn = sheet.getColumn(startCellColumn);

  if (!endColumn) throw new Error("End column not found");
  if (!startColumn) throw new Error("Start column not found");

  endColumn = endColumn._number;
  startColumn = startColumn._number;

  const cells = [];
  for (let y = startRow; y <= endRow; y++) {
    const row = sheet.getRow(y);

    for (let x = startColumn; x <= endColumn; x++) {
      cells.push(row.getCell(x));
    }
  }

  return cells;
};

Sample Usage

const lastRow = worksheet.lastRow._number;
const row = selectRange(worksheet, `startColumnKey:${lastRow}`,  `endColumnKey:${lastRow + 4}`);

row.forEach(cell => {
  // TODO: Do something with each cell
});
2reactions
JacobWeisenburgercommented, Oct 23, 2017

so…I’m guessing this is not a problem for anyone else… sigh

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select specific cells or ranges - Microsoft Support
Hold down the Ctrl key, and left-click each cell or range you want to include. If you over select, you can click on...
Read more >
How to Select a Range of Cells in Microsoft Excel | Webucator
Click on a cell in one of the corners of the range of cells you wish to select. Selected Cell · Hold the...
Read more >
How to Easily Select a Block of Cells in Excel - How-To Geek
One of the easiest ways to select a range of cells is by clicking and dragging across the workbook. Click the first cell...
Read more >
How to Select Cells Range/Rows/Columns in Excel - iSumsoft
In a worksheet, click the first cell in the range, and then hold down Shift while you click the last cell in the...
Read more >
How do I select a range of cells in an Excel formula? - Quora
3.Once you reach the first cell in the range, simple hold the shift button and keep pressing the down key(If its a range...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found