0

I'm working on some code, mostly just playing around, with the Office-js API (v1.1), trying to do some things. I can take code examples and run them just fine, but I don't know Javascript well enough to know what I'm doing.

I took an example of enumerating tables and am trying to add some things to it, but it's not working and I don't know why. Can anyone help me out here?

The code:

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

In the console log I get this message:

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

I'm basing this off code found here: http://officesnippetexplorer.azurewebsites.net/#/snippets/excel (select 'Table', and snippet 'Get tables in workbook'). Any help would be greatly appreciated!

Thanks, Zack Barresse

Zack Barresse
  • 239
  • 1
  • 11

1 Answers1

4

I don't think you mean to change tables.count, do you?

That's what the error is telling you - you have:

 if (tables.count = 0) {

but you really wanted:

 if (tables.count == 0) {

The first tries to set tables.count to 0, the second tests if tables.count is equal to 0.

cco
  • 5,873
  • 1
  • 16
  • 21
  • Awesome, thanks so much! That indeed worked just fine. As a follow-up question (I can post a new one if need be, just let me know if that is the nettiquette 'round these parts), how would I format the index number being posted to the console log? For example, I want it always formatted with two digits. I also see it is zero-based, which I'll need to find how to add a number to it lol. – Zack Barresse Sep 25 '16 at 00:41
  • Google is your friend for that - http://stackoverflow.com/questions/8043026/javascript-format-number-to-have-2-digit is just the first link I found when searching "javascript format number" (taking google's suggestion of adding "to 2 digits" to my query). Programming languages are good at adding one number to another; you should find a programming tutorial you like to learn more about that. – cco Sep 25 '16 at 04:36