首页 > 解决方案 > 无法在 Excel Web 加载项中创建表格

问题描述

我写了一个函数来创建表

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


错误:出了点问题!!!RichApi.Error:参数无效或丢失或格式不正确。在bfprocessRequestExecutorResponseMessage ( https://appsforoffice.microsoft.com/lib/1.1/hosted的新 c ( https://appsforoffice.microsoft.com/lib/1.1/hosted/excel-web-16.00.js:24:293355 ) /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")
});

}

标签: office365office-jsoffice-addinsexcel-addins

解决方案


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


推荐阅读