无法在 excel Web 插件中创建 table

Unable to create table in excel web add-in

我写了一个函数来创建table

调查问题后:- 问题是代码在一个 sheet 上添加名称为 "tableName" 的 table 时工作正常,但是如果我在另一个 sheet 上插入另一个 table 具有相同名称 "tableName" 它抛出一个错误并且不插入 table 但没有内容。 我需要将名称应用于 table,因为我想根据需要检索 table,并且我是在其 name.Read 一篇文章 on table naming 的帮助下完成的,但是如果我重命名 sheet 名称然后如何访问它。


错误: 出问题了!!! 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.invoke 任务 (https://localhost/excelProject/polyfills.js:2785:31) 在 Object.onInvoke任务 (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", "0"],
      ["1/2/2017", "Northwind Electric Cars", "Transportation", "2"],
      ["1/5/2017", "Best For You Organics Company", "Groceries", ""],
      ["1/10/2017", "Coho Vineyard", "Restaurant", ""],
      ["1/11/2017", "Bellows College", "Education", "0"],
      ["1/15/2017", "Trey Research", "Other", "5"],
      ["1/15/2017", "Best For You Organics Company", "Groceries", ""]
  ]);



  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")
});

}

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