0

IIn excel, using an office script, I'm trying to copy a value from one cell in what I assume is a valid range to another. Simplified version of my code:

  let selectedCell = workbook.getActiveCell();
  let selectedSheet = workbook.getActiveWorksheet();
  let range = selectedSheet.getUsedRange();
  let visibleRange = range.getVisibleView();
  let rangeValues = visibleRange.getValues();  // This doesn't fail, so I assume it's a valid range.

  let rowCount = visibleRange.getRowCount();

  for (let idx = 1; idx<rowCount; idx++) {
    visibleRange("L"+idx).copyFrom(visibleRange("J"+idx));
  }
}

I get this error:

visibleRange is not a function. (In 'visibleRange("L" + idx)', 'visibleRange' is an instance of r)

... and I am not sure why. I started with addressing the visibleRange numerically but I got the same error. Any suggestions would be helpful!

Thanks in advance.

Scott.

1
  • The error is giving you everything. Using () brackets is typescript notation for calling a function. It’s not a function, it’s an object. You’ll need to use a method from that object type … learn.microsoft.com/en-us/javascript/api/office-scripts/… Commented Jan 5, 2024 at 0:07

1 Answer 1

0
  • getVisibleView() returns an Excel.RangeView object, it is different with Range object.
  • Use getSpecialCells(ExcelScript.SpecialCellType.visible) to get the visible range. Please note that it's often a non-continuous range.
function main(workbook: ExcelScript.Workbook) {
  let selectedCell = workbook.getActiveCell();
  let selectedSheet = workbook.getActiveWorksheet();
  let range = selectedSheet.getUsedRange();
  let visibleRange = range.getSpecialCells(ExcelScript.SpecialCellType.visible);
  visibleRange.getAreas().forEach(areaRange => {
    let rowCount = areaRange.getRowCount();
    // console.log(areaRange.getAddress());
    let startRow = areaRange.getCell(0, 0).getRowIndex() + 1;
    for (let idx = 1; idx < rowCount; idx++) {
      selectedSheet.getRange("L" + (idx + startRow)).copyFrom(selectedSheet.getRange("J" + (idx + startRow)))
    }
  })
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.