OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
677 stars 95 forks source link

GeneralException when copying range from/to column H #4715

Closed manuelsidler closed 1 month ago

manuelsidler commented 3 months ago

Your Environment

Expected behavior

When copying a range from/to column H, I'd expect it to work like every other column.

Current behavior

When using column H explicitly for a range, OfficeJS throws a GeneralException.

Code snippet:

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    const range = 'A1:H1' // more examples that won't work: 'H1', 'H1:M2', 'A1:H2', ...
    const sourceRange = sheet.getRange(range)

    sheet.getRange().copyFrom(sourceRange, Excel.RangeCopyType.all)

    await context.sync();
  });

Exception:

code: "GeneralException"
traceMessages: Array[0]
innerError: null
debugInfo: Object
code: "GeneralException"
message: "There was an internal error while processing the request."

The error occurs in Excel on Mac, Windows and on the web

Steps to reproduce

  1. Open ScriptLab in Excel on Windows, Mac or on the web
  2. Insert the code snippet above
  3. Run
shanshanzheng-dev commented 3 months ago

Hi @manuelsidler Thanks for reporting. About this API, you can follow this reference: https://learn.microsoft.com/en-us/javascript/api/excel/excel.range?view=excel-js-preview#excel-excel-range-copyfrom-member(1),
you could try this code snippet, seem you have to set an initial range:

async function run() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    const range = "A1:H1"; // more examples that won't work: 'H1', 'H1:M2', 'A1:H2', ...
    const sourceRange = sheet.getRange(range);

    sheet.getRange("A2").copyFrom(sourceRange, Excel.RangeCopyType.all);

    await context.sync();
  });
}

Please let me know if resolve your problem. Thanks.

manuelsidler commented 3 months ago

@shanshanzheng-dev using an initial range helps, thank you. But I still don't understand why the error occurs only when I explicitly use the "H" column for the range. The snippet I posted works fine with ranges like A1:K1, G1:I1 and so on (without specifying an initial range):

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();

    const range = 'A1:I1' // works without specifying an initial range
    const sourceRange = sheet.getRange(range)

    sheet.getRange().copyFrom(sourceRange, Excel.RangeCopyType.all)

    await context.sync();
  });
shanshanzheng-dev commented 3 months ago

Hi @manuelsidler Thanks for confirming. I can repro this problem. We'll be looking into the issue and report back if we have a suggestion for you. Thanks.

shanshanzheng-dev commented 1 month ago

Hi @manuelsidler Thanks for your patience. After discussing with experts in this area, we think the behavior is by design.

The by design behavior is that when the target area is an exact multiple of the source, the source content will be automatically repeated to fill in the whole target area.

For example, firstly select A1:B4 and copy into clipboard. Select D1:I12, and paste. image image

So this is expected.

If the add-in developer tries to copy an area that could fit into the whole grid repeatedly, it will trigger this behavior.

Solution: If you want to paste only one copy of the source, the option is to paste it to a single cell and excel will expand the area and paste the whole range to it. If want to fill in the target area repeatedly, the size of target area cannot be too large that could cause an out of memory error.

microsoft-github-policy-service[bot] commented 1 month ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

microsoft-github-policy-service[bot] commented 1 month ago

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.