【问题标题】:Unable to create table in excel web add-in无法在 Excel Web 加载项中创建表格
【发布时间】:2020-03-24 08:45:03
【问题描述】:

我写了一个函数来创建表

在对问题进行调查后:- 问题是代码在一张表上添加名称为“tableName”的表时工作正常,但是如果我在另一个表上插入另一个表,名称为“tableName”,它会引发错误并且不会插入表但没有内容。 我需要将名称应用于表,因为我想根据需要检索表,并且我正在借助它的名称来执行此操作。阅读文章 on table naming 但如果我重命名工作表名称,那么如何访问它。


错误: 出问题了!!! RichApi.Error:参数无效或丢失或格式不正确。 在新 c (https://appsforoffice.microsoft.com/lib/1.1/hosted/excel-web-16.00.js:24:293355) 在 b.f.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1.1/hosted/excel-web-16.00.js:24:354008) 在https://appsforoffice.microsoft.com/lib/1.1/hosted/excel-web-16.00.js:24:352113 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (https://localhost/excelProject/polyfills.js:2753:26) 在 Object.onInvoke (https://localhost/excelProject/vendor.js:57267:33) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (https://localhost/excelProject/polyfills.js:2752:52) 在 Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (https://localhost/excelProject/polyfills.js:2512:43) 在https://localhost/excelProject/polyfills.js:3251:34 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (https://localhost/excelProject/polyfills.js:2785:31) 在 Object.onInvokeTask (https://localhost/excelProject/vendor.js:57258:33)


代码

createTable(tableName, startColumn, endColumn, headerData, bodyData, onTableChangedCallback, onSelectionChangedCallback){
Excel.run((context)=> {
  var sheet = context.workbook.worksheets.getActiveWorksheet();
  let colRange = startColumn+":"+endColumn;//like B2:D2
  var sfTable = sheet.tables.add(colRange, true /*hasHeaders*/);
  sfTable.name = "tableName";
  let headerRow = [];
  headerRow.push(headerData);//Below code is hardcoded for 4 column for now


  var tableHeaderRange = sfTable.getHeaderRowRange();
  tableHeaderRange.values = 
  [["Date", "Merchant", "Category", "Amount"]];

  sfTable.rows.add(null /*add rows to the end of the table*/, 
    [
      ["1/1/2017", "The Phone Company", "Communications", "$120"],
      ["1/2/2017", "Northwind Electric Cars", "Transportation", "$142"],
      ["1/5/2017", "Best For You Organics Company", "Groceries", "$27"],
      ["1/10/2017", "Coho Vineyard", "Restaurant", "$33"],
      ["1/11/2017", "Bellows College", "Education", "$350"],
      ["1/15/2017", "Trey Research", "Other", "$135"],
      ["1/15/2017", "Best For You Organics Company", "Groceries", "$97"]
  ]);



  if (Office.context.requirements.isSetSupported("ExcelApi", "1.2")) {
      sheet.getUsedRange().format.autofitColumns();
      //sheet.getUsedRange().format.autofitRows();
      sheet.getUsedRange().format.rowHeight = 15;
  }
  sfTable.onChanged.add(onTableChangedCallback);
  sfTable.onSelectionChanged.add(onSelectionChangedCallback)
  sheet.activate();

  return context.sync(); 
})
.catch((err)=>{
  this.errorHandlerFunction(err,"create table")
});

}

【问题讨论】:

  • 我无法重现此问题,我已尝试在我的代码中使用您的功能,您能否在必要时查看并更新以重现此问题?谢谢这是我的要点:gist.github.com/lumine2008/b3d9261f0c6dc9f6a31078fa1b72a8dc
  • 你能在每张 excel web 表上创建表格吗??
  • 是的,我可以用我的 gist 成功创建表格,你能试试这个 gist:gist.github.com/lumine2008/b3d9261f0c6dc9f6a31078fa1b72a8dc
  • @RaymondLu 请阅读问题我已经改变了几点..感谢您的努力。
  • 要创建2个同名表吗?

标签: office365 office-js office-addins excel-addins


【解决方案1】:

错误的原因非常不寻常。但是问题在于具有相同名称的表。当我以不同的方式命名它们时,错误被删除了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    相关资源
    最近更新 更多