Trying to do maintenance on a table via an add-in. If the table has more than N rows, I want to delete all but N from the end.
I finally got it to work in Script Lab as demonstrated in this gist: https://gist.github.com/70d3d62d32d4fe8f34279ab240f7b110
but it doesn't feel like I actually understand why it is working.
- I have to call sync, but doesn't matter if I call it before or after the delete call.
- I thought it made sense to wrap the delete inside a
context.sync().then(()=>obj.delete);
, but that doesn't work. - I haven't seen samples that make a bare call to
context.sync()
, they usually callawait context.sync()
, but when I try to do that, IntelliSense says it isn't in an async function. If I add the async keyword to that nested closure, then the script throws a runtime error saying that doesn't work and I need to manage the binding manually...
Could someone please review this code and help clarify these questions?
If you haven't already done so, I'd suggest that you review Excel JavaScript API core concepts, which contains info about
Excel.run(...)
,context.sync()
, andload()
.Additional comments/info:
As described in the article, you must call
load()
before you can read properties of an object. You don't need to callload()
if you're simply setting property values or accessing other methods off of a property. If you do need to callload()
(because you're intending to read the properties of the object), you'll need to subsequently callsync()
so that theload()
instruction gets executed, before you can read the object's properties.As described in the article, you can queue up multiple instructions on the
context
before you callcontext.sync()
to execute those instructions. In thedeleteRows()
function of your snippet, thecontext.sync()
that you've prefaced with a comment isn't necessary because you can simply add thelastThreeRows.delete()
instruction to the queue first, and then all of the instructions will be executed at once at the end ofExcel.run
. (It's best practice to explicitly callcontext.sync()
at the end of anExcel.run
, but technically you don't have to because any queued instructions will automatically get executed whenExcel.run
concludes.)If you're using TypeScript, you should always
await
thecontext.sync()
(e.g.,await context.sync();
), and the function definition for the function that containsawait context.sync()
must begin with the keywordasync
(e.g.,async function foo() {...}
). Within the body of a function, you can only use the keywordawait
if the function is defined asasync
.Here's an updated gist that you can import to and run in Script Lab: https://gist.github.com/kbrandl/74e64d74f396cbe6047b3247e3c066d7. I've updated code within the
deleteRows()
function to reflect the concepts that I've described above.