【问题标题】:How to add comment in using office js in window office js 2016?如何在 window office js 2016 中使用 office js 添加评论?
【发布时间】:2021-11-10 12:16:01
【问题描述】:

我正在使用 office js。在单元格中添加了我的加载项评论与 mac office 和 office 365 一起正常工作,但我无法在 windows pc office 哪个版本的 office 2016 中插入 cmets。 我做错了什么?请指导我谢谢。这是我的代码 sn-p

await Excel.run(async (ctx) => {
   let wb = ctx.workbook;
    await ctx.sync().then(async () => {
            try {
                const address="sheet!A4"
              var comment = wb.comments.getItemByCell(address);
              comment.delete();
              wb.comments.add(address, "This is simple test comment");
            } catch (error) {
              if (error.code == Excel.ErrorCodes.itemNotFound) {
                wb.comments.add(address, "This is simple test comment");
                console.log("Add comment successfully!");
              }
            }
          
        });
        await ctx.sync();
    });

【问题讨论】:

  • 这个await ctx.sync().then(async () => 看起来很乱。您可以删除等待并检查吗?
  • 我已经删除然后我检查了它仍然无法正常工作。

标签: office365 ms-office office-addins


【解决方案1】:

我没有安装 Excel 2016 进行测试,但我确实看到了一些问题,当我更正这些问题并在 Excel 365 中运行时,一切正常。

首先,您的范围或“地址”常量有问题。因为它是在“try”块中声明的,所以它在“catch”块中不可用。要么将其声明移到 try/catch 结构之上,要么将其从“const”更改为“var”。

我看到的第二个问题是,如果因为 Sheet1 的单元格 A4 上没有注释而出现错误,则在执行 ctx.sync() 语句之前不会发生错误,这在“try”之外块,因此不会调用“catch”。这是在 Excel 365 上运行良好的更正代码。

function test_comment(){
  Excel.run(async (ctx) => {
    let wb = ctx.workbook;
    try {
      var address="Sheet1!A4"
      var comment = wb.comments.getItemByCell(address);
      comment.delete();
      wb.comments.add(address, "This is simple test comment");
      await ctx.sync();
      console.log("Replace comment successfully!");
    } catch (error) {
      if (error.code == Excel.ErrorCodes.itemNotFound) {
        wb.comments.add(address, "This is simple test comment");
        await ctx.sync();
        console.log("Add comment successfully!");
      }
    }
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多