Office Script - "Workbook getActiveWorksheet: An internal error has occurred."

Anonymous
2023-11-22T15:51:17.4166667+00:00

Hi everyone,

I'm running a simple script in my Excel Web and I received an error message below:

User's image


Here's the script I'm using:

function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    // Get Dynamic Range
    let myRange = selectedSheet.getRange("A4").getSurroundingRegion().getAddress();
    // Paste to myRange on selectedSheet from range A4:K4 on selectedSheet
    selectedSheet.getRange(myRange).copyFrom(selectedSheet.getRange("A4:K4"), ExcelScript.RangeCopyType.formats, false, false);
    // Custom sort on range spanned by auto filter
    selectedSheet.getAutoFilter().getRange().getSort().apply([{ key: 8, ascending: true }], false, true);
    // Set range A5:A6 on selectedSheet
    selectedSheet.getRange("A4:A5").setValues([["1"], ["2"]]);
    // Auto fill range
    selectedSheet.getRange("A4:A5").autoFill();
}

Can anyone advise on how to fix this?

Thank you very much.

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiajing Hua 18,060 Reputation points Moderator
    2023-11-23T01:59:11.94+00:00

    Hi @Jared

    As your issue is related to code development, I also suggest you post new threads on other forums on this link: Excel add-ins developer community.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.