脚本lab显示了如何使用行、列和单元格值获取区域的示例:https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-ranges-advanced
但是,示例中的所有值都是硬编码的。我在任何页面上都找不到任何如何在get范围内使用变量的示例?也许这是一个简单的javascript东西,但我对它完全是新手。有没有人可以分享一个怎么做的例子?sheet.getRange("4: 9 ")对数字4和数字9使用变量?
如果你确实知道上面的答案,你也可以分享一下,如何在下面的例子中使用单元格引用来做到这一点吗?
假设我可以找到行名和列名的值,并在一些变量中设置它们。如何在下面的代码中使用它来替换G1、A1和E1的值?sheet.getRange("G1").copyFrom("A1:E1");
提前感谢您的帮助!
附言:我已经尝试使用“脚本实验室范围变量”的关键字搜索堆栈溢出,但没有找到答案。所以才会在这里问。
Excel.run(function (context) {
var sheet = context.workbook.worksheets.getItem("Sample");
// Group the larger, main level. Note that the outline controls
// will be on row 10, meaning 4-9 will collapse and expand.
sheet.getRange("4:9").group(Excel.GroupOption.byRows);
// Group the smaller, sublevels. Note that the outline controls
// will be on rows 6 and 9, meaning 4-5 and 7-8 will collapse and expand.
sheet.getRange("4:5").group(Excel.GroupOption.byRows);
sheet.getRange("7:8").group(Excel.GroupOption.byRows);
// Group the larger, main level. Note that the outline controls
// will be on column R, meaning C-Q will collapse and expand.
sheet.getRange("C:Q").group(Excel.GroupOption.byColumns);
// Group the smaller, sublevels. Note that the outline controls
// will be on columns G, L, and R, meaning C-F, H-K, and M-P will collapse and expand.
sheet.getRange("C:F").group(Excel.GroupOption.byColumns);
sheet.getRange("H:K").group(Excel.GroupOption.byColumns);
sheet.getRange("M:P").group(Excel.GroupOption.byColumns);
return context.sync();
}).catch(errorHandlerFunction);发布于 2020-03-27 05:55:47
所以,我在这里得到了答案:https://github.com/OfficeDev/office-js-docs-pr/issues/1699
感谢AlexJerabek (你可以在上面的github链接上找到他的id )。
总之: getRange方法接受表示工作表中单元格范围的字符串,例如"A1:D4“、"A:D”表示列,或"1:4“表示行。只要变量映射到Excel行或列,就可以使用字符串连接来生成参数。您可能需要使用range.columnIndex、range.rowIndex和range.getCell将从零开始的数字转换为基于字母的列,或者从零开始的数字转换为基于字母的列。
基于这个答案,我尝试了下面(并且成功了):
const sheet = context.workbook.worksheets.getActiveWorksheet();
var firstrow = 1
var lastRow = 6
var range = sheet.getRange(firstrow + ":" + lastRow)https://stackoverflow.com/questions/60873368
复制相似问题