OfficeDev / office-js-docs-reference

Microsoft Office JavaScript API Reference
https://learn.microsoft.com/javascript/api/overview
MIT License
120 stars 67 forks source link

What is the result of a call to Range.find() when the searched text is not found? #1785

Open ChemMitch opened 10 months ago

ChemMitch commented 10 months ago

Hello MIcrosoft Team,

I'm using the find method to look for text and seeing a RichApi.Error with code ItemNotFound.

Is that the expected behavior when the specified text is not found?

Thanks in advance!


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AlexJerabek commented 10 months ago

Hi @ChemMitch,

Yes, that is the expected behavior. There's a comment to that effect in the [Range.find sample(https://learn.microsoft.com/en-us/javascript/api/excel/excel.range?view=excel-js-preview#excel-excel-range-find-member(1)). To avoid an error being thrown, you can instead use the Range.findOrNullObject method. That returns an object with isNullObject property to check. More information about the *OrNullObject pattern can be found here.

The base description of Range.find should specific the error throwing. I'll leave this issue open until that's resolved. @alison-mk. could you please follow up with that?

ChemMitch commented 10 months ago

Thank you once again, Alex!

A follow-up question. Given this code

export async function getVocabularySheet(workbook: Excel.Workbook): Promise { return await Excel.run(async (context) => { let vocabSheet: Excel.Worksheet = workbook.worksheets.getItemOrNullObject(VOCABULARY_SHEET_NAME); vocabSheet.load("address"); await context.sync(); if (vocabSheet.isNullObject) { vocabSheet = workbook.worksheets.add(VOCABULARY_SHEET_NAME); vocabSheet.visibility = Excel.SheetVisibility.hidden; console.log("created vocabulary sheet"); } return vocabSheet; }); }

this error occurs:

The property 'isNullObject' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.

But I'm doing what they say! I've tried calling vocabSheet.load("isNullObject"); and the same error occurs (plus a warning about performance.

So, what's the correct way to access isNullObject?

AlexJerabek commented 10 months ago

Hi @ChemMitch,

That should be working. I put your code into Script Lab and it seems to be working fine. Here's the gist.

junaid042 commented 10 months ago

@ChemMitch You need to load isNullObject' First ten you can use it.

for example

vocabSheet.load(['isNullObject']); await context.sync(); //now you can use isNullObject

if (vocabSheet.isNullObject) {}

ChemMitch commented 10 months ago

Thanks, @junaid042 . I did that but got the error anyhow. I've seen that happen a lot.

Rick-Kirkham commented 10 months ago

You never need to load isNullObject. That property should always load even if you don't explicitly load it with the load() method.

Please import Alex's gist into Script Lab and see if it works for you.

ChemMitch commented 10 months ago

I can run Alex's gist in script lab fine. The case I'm trying to run within Excel now is a little different.
Is isNullObject supported on a Range? I get errors consistently invoking it.

Rick-Kirkham commented 10 months ago

The code in your earlier comment shows that you are calling isNullObject on a Worksheet object, not a Range object.