2016-09-23 151 views
0

我正在使用Office-js API(v1.1)尝试执行一些操作,其中的一些代码大多只是在玩耍。我可以使用代码示例并运行它们,但是我不太了解JavaScript以了解我在做什么。枚举表和列表名称

我拿了一个枚举表的例子,并试图添加一些东西给它,但它不工作,我不知道为什么。有人可以帮我从这里出去吗?

代码:

Excel.run(function (ctx) { 

    var tables = ctx.workbook.tables; 
    var tableNames = ctx.workbook.tables.load("name"); 

    return ctx.sync().then(function() { 

     console.log("Tables in workbook:"); 

     if (tables.count = 0) { 
      console.log("No tables found"); 

     } else { 
      for (var i = 0; i < tableNames.items.length; i++) 
      { 
       console.log(i + ": " + tableNames.items[i].name); 
      } 
     } 
     console.log("------------------------"); 
    }); 

}).catch(function (error) { 
    console.log(error); 
}); 

在控制台日志中我得到这个消息:

Tables in workbook: 
TypeError: Assignment to read-only properties is not allowed in strict mode 

我立足这一关代码在这里找到:http://officesnippetexplorer.azurewebsites.net/#/snippets/excel(选择“表格”和段“在工作簿中获取表格)。任何帮助将不胜感激!

感谢, 扎克Barresse

回答

4

我不认为你的意思是改变tables.count,你呢?

这是什么错误是告诉你 - 你有:

if (tables.count = 0) { 

,但你真的想:

if (tables.count == 0) { 

首先尝试设置tables.count0,第二测试,如果tables.count等于到0

+0

太棒了,非常感谢!这确实很好。 作为一个后续问题(如果需要,我可以发布一个新的问题,只要告诉我,如果这是nettiquette围绕这些部分),我将如何格式化发布到控制台日志的索引号?例如,我希望它总是用两位数字格式化。 我也看到它是基于零的,我需要找到如何向它添加一个数字lol。 –

+0

谷歌是你的朋友 - http://stackoverflow.com/questions/8043026/javascript-format-number-to-have-2-digit只是我搜索“JavaScript格式数字”时发现的第一个链接(以谷歌的建议在我的查询中添加“2位数字”)。编程语言擅长将一个数字添加到另一个数字;你应该找到一个编程教程,你想了解更多。 – cco