选择列时无法获取选定的范围值 - Excel Javascript

问题描述 投票:0回答:1

我想知道我是否发现了另一个使用“整个列范围”的错误。我希望捕获选定的范围值,通常,当我选择范围时,我只需选择列标题“A”,它就会选择整个列。但是,在浪费时间测试之后,我意识到我的代码仅在我不选择列时才有效。

这是一个错误还是我的代码不正确?

有没有办法深入到“选定的值范围”?就像你可以如何做

getUsedRange(true)
一样,
true
是“仅使用带有值的范围”。

这有效:

console.log('A1 Select')
var ws = context.workbook.worksheets.getActiveWorksheet();
ws.getRange("A1").select()
await context.sync()

var range = context.workbook.getSelectedRange();
range.load(['values']);
await context.sync();

var firstSelectedCellValue = range.values[0][0];
console.log('firstSelectedCellValue:')
console.log(firstSelectedCellValue)
await context.sync();

这不起作用:

console.log('A Col Select')
ws.getRange("A:A").select()
await context.sync();

var range = context.workbook.getSelectedRange();
range.load(['values']);
await context.sync();

var firstSelectedCellValue = range.values[0][0];
console.log('firstSelectedCellValue:')
console.log(firstSelectedCellValue)
await context.sync();

这是输出:

HTML1300: Navigation occurred.
index.html
Dev_Mode = true
Agave.HostCall.IssueCall
Agave.HostCall.ReceiveResponse
commands.js:Office.onReady
Agave.HostCall.IssueCall
commandsfunc
A1 Select
Agave.HostCall.ReceiveResponse
firstSelectedCellValue:
Header A
A Col Select
Error: 
TypeError: Unable to get property '0' of undefined or null reference
   {
      [functions]: ,
      __proto__: { },
      description: "Unable to get property '0' of undefined or null reference",
      message: "Unable to get property '0' of undefined or null reference",
      name: "TypeError",
      number: -2146823281,
      stack: "TypeError: Unable to get property '0' of undefined or null reference
   at Anonymous function (http://localhost:3000/yo/dist/commands.js:1:1453)
   at h (http://localhost:3000/yo/dist/globals.js:1:1139)
   at Anonymous function (http://localhost:3000/yo/dist/globals.js:1:948)
   at Anonymous function (http://localhost:3000/yo/dist/globals.js:1:1583)
   at n (http://localhost:3000/yo/dist/commands.js:1:52)
   at u (http://localhost:3000/yo/dist/commands.js:1:265)
   at Anonymous function (http://localhost:3000/yo/dist/polyfill.js:1:76119)
   at e (http://localhost:3000/yo/dist/polyfill.js:1:31843)",
      Symbol()_7.l9joxxhdsmj: undefined,
      Symbol(nodejs.util.inspect.custom)_j.l9joxxhdsja: undefined
   }
excel office-js
1个回答
1
投票

好吧,我无法读取全范围值这一事实与

selectedRange
无关。我以前也曾沿着这条路走下去,基本上开发人员告诉他们,他们不在乎它是否按照您的预期工作,因为这将是一个缓慢的操作,并且需要调整用户代码,而不是 API .

您无法读取完整的列或行值。它将返回

null
。您需要检查
isEntireRow
isEntireColumn
,然后从范围中删除一行/列,然后进行检查。然后,您可以运行另一个操作来检查删除的单行/列(这比根据开发人员修复 API 更好..)

如果用户无论如何都选择了整个列/行,我计划深入到

usedRange
,所以下面是我最终得到的结果,我将其包装到一个函数中以获得“选定范围”。

var Load_Opts_Arr = ['rowCount','rowIndex', 'columnCount','columnIndex', 'values','isEntireColumn','isEntireRow']  

var rng = context.workbook.getSelectedRange();
rng.load(Load_Opts_Arr);
await context.sync();

if (rng.isEntireColumn == true || rng.isEntireRow == true) {
    var ws = context.workbook.worksheets.getActiveWorksheet();
    var Used_Rng_And_Props = ws.getUsedRange(true)
    Used_Rng_And_Props.load(['rowCount','rowIndex', 'columnCount','columnIndex'] );
    await context.sync();
}

if (rng.isEntireColumn == true) {
    var rng = ws.getRangeByIndexes(Used_Rng_And_Props.rowIndex, rng.columnIndex, Used_Rng_And_Props.rowCount, rng.columnCount)
    rng.load(Load_Opts_Arr);
    await context.sync();
}

if (rng.isEntireRow == true) {
    var rng = ws.getRangeByIndexes(rng.rowIndex, Used_Rng_And_Props.columnIndex, rng.rowCount, Used_Rng_And_Props.columnCount - rng.columnIndex)
    rng.load(Load_Opts_Arr);
    await context.sync();
}
© www.soinside.com 2019 - 2024. All rights reserved.