Office-js:如果选择了单个单元格,如何查找同一列中的最后一个单元格?

Office-js: If a single cell is selected, how can I lookup the last cell in the same column?

在 Excel 2016 中使用 Office.js,如果用户选择了一个单元格,我如何获取与当前单元格在同一列中的最后一个单元格(包含数据)的地址已选择用户?

 ________
|    A   |
----------
|        |
----------
|  Title |
----------
|   3    |    <----- user clicks here
----------
|   6    |  
----------
|   77   |    <----- how can I lookup this address?
----------
|        |
----------
|        |
----------

所以我可以得到数组 [ [3], [6], [77] ]

以下是我如何查找用户所选单元格的地址:

Excel.run(async (ctx) => {
    let range = ctx.workbook.getSelectedRange().getUsedRange();
    range.load("address");
    await ctx.sync();
});

在这种情况下应该 return 地址:"Sheet1:A3"
我希望能够到达:"Sheet1:A3:A5".

你很接近;但您需要的是从包含选择的整个列中获取使用范围:

    await Excel.run(async (context) => {
        let range = context.workbook.getSelectedRange();
        let usedRange = range.getEntireColumn().getUsedRange();
        usedRange.load("address");
        await context.sync();

        OfficeHelpers.UI.notify(usedRange.address);
    })

您可以在新的 Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/Zlatkovsky/1393689cc63c002165e06fa6fb4eb755. See more info about importing snippets to Script Lab.

中单击五下即可实时试用此代码段

添加到@MichaelZlatkovsky 的回答,这也可能有用:
https://github.com/OfficeDev/office-js-docs/blob/master/reference/excel/range.md#getlastcell

文档-getLastCell():

Gets the last cell within the range. For example, the last cell of "B2:D5" is "D5".

所以是这样的:

var range = range.getEntireColumn().getUsedRange().getLastCell();