Closed manuelsidler closed 1 month 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.
@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();
});
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.
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.
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.
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!
This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.
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:
Exception:
The error occurs in Excel on Mac, Windows and on the web
Steps to reproduce