即使 Column 为空,也可以使用 Office-JS 获取 Column rowCount

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

注意:根据 MSFT 的建议,我想避免额外的

context.sync()
,但我认为这可能是唯一的方法?或者在函数内部捕获错误?

我需要获取函数的当前 rowCount,但如果该行为空,我会收到“ItemNotFound”错误。

如何检查可能有或可能没有数据的列的行数?

注意:我正在测试一个全新的 WS,这是我目前所拥有的

var ws = context.workbook.worksheets.getActiveWorksheet();
var col_one_rng = ws.getRange("A:A").getUsedRange(true)
col_one_rng.load(['rowCount'])
console.log('BeforeSync')
await context.sync(); //Error
console.log('AfterSync')
var col_one_pre_row_count = col_one_rng.rowCount
console.log('col_one_pre_row_count:' + col_one_pre_row_count)

完整的错误堆栈:

ItemNotFound: The requested resource doesn't exist.
   {
      [functions]: ,
      __proto__: { },
      code: "ItemNotFound",
      data: undefined,
      debugInfo: { },
      description: "The requested resource doesn't exist.",
      httpStatusCode: 404,
      innerError: null,
      message: "The requested resource doesn't exist.",
      name: "RichApi.Error",
      stack: "ItemNotFound: The requested resource doesn't exist.
   at Anonymous function (https://appsforoffice.microsoft.com/lib/beta/hosted/excel-win32-16.01.js:26:306238)
   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.yx7nmjnyzes: undefined,
      Symbol(nodejs.util.inspect.custom)_j.yx7nmjnyzho: undefined,
      traceMessages: [ ]
   }

更新:

如果我删除

getUsedRange(true)
然后我得到 rowCount =
1048576
。但是不要尝试使用整个列范围,因为 Excel 工程师已经决定你不应该这样做,因为他们最了解你不应该那样做,所以你必须通过 hack 来“变通” --> Excel JavaScript API 不能使用整个列范围

excel office-js
© www.soinside.com 2019 - 2024. All rights reserved.