1

I am trying to understand why getRangeByIndex does not work when copy pasting from one sheet to another sheet

Here is the getRange script

function main(workbook: ExcelScript.Workbook) {
    let temp = workbook.getWorksheet("Temp");
    let selectedSheet = workbook.getActiveWorksheet();
    // Paste to range A1 on temp from range F9:J9 on selectedSheet
    temp.getRange("A1").copyFrom(selectedSheet.getRange("F9:J9"), ExcelScript.RangeCopyType.all, false, false);
}

Here is the getRangeByIndex

function main(workbook: ExcelScript.Workbook) {
    let temp = workbook.getWorksheet("Temp");
    let selectedSheet = workbook.getActiveWorksheet();
    // Paste to range A1 on temp from range F9:J9 on selectedSheet
    temp.getRange("A1").copyFrom(selectedSheet.getRangeByIndexes(0,5,1,5), ExcelScript.RangeCopyType.all, false, false);
}

1 Answer 1

0
  • getRangeByIndexes(8, 5, 1, 5) refers to the range F9:J9

Microsoft documentation:

ExcelScript.Worksheet interface

function main(workbook: ExcelScript.Workbook) {

    let temp = workbook.getWorksheet("Temp");
    let selectedSheet = workbook.getActiveWorksheet();
    // Paste to range A1 on temp from range F9:J9 on selectedSheet
    console.log(selectedSheet.getRangeByIndexes(8, 5, 1, 5).getAddress())
    temp.getRange("A1").copyFrom(selectedSheet.getRangeByIndexes(8,5,1,5), ExcelScript.RangeCopyType.all, false, false);

}

Question: F9 to J9 of sheet 1 to A1 of sheet 2, then F10 to J10 of sheet 1 to A2 of sheet2, then F11 to J11 of sheet 1 to A3 of sheet 2 and so on

function main(workbook: ExcelScript.Workbook) {

    let temp = workbook.getWorksheet("Temp");
    let selectedSheet = workbook.getActiveWorksheet();
 
    const rowCnt = 11; // Copy F9:J19 to A1:E11
    temp.getRange("A1").copyFrom(selectedSheet.getRangeByIndexes(8,5,rowCnt,5), ExcelScript.RangeCopyType.all, false, false);

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

5 Comments

This worked. Thank you. I learned 2 things from here,console and the indexing
Hi Taller, is there anyway that .getRange("A1") would work for getRangeByIndex(0,0,1,1) ? I am trying to make it dynamic
What's kind of dynamic? Pls share a sample scenario.
basically, I want to be able to copy from Column F to J of sheet 1moving down a row for every increment of row to destination cell of sheet 2. like F9 to J9 of sheet 1 to A1 of sheet 2, then F10 to J10 of sheet 1 to A2 of sheet2, then F11 to J11 of sheet 1 to A3 of sheet 2 and so on
You don't need to use looping. Believe it or not, it's a fairly simple method. I've uploaded a snippet. Hope it's helpful for you.

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.