Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ChemMitch opened this issue Jan 4, 2024 · 8 comments
Assignees
Labels
Area: Excel Feedback on Excel content Type: doc request Request for new documentation or updates/enhancements to existing documentation

Comments

@ChemMitch
Copy link

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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 new issue, needs PM on rotation to triage asap label Jan 4, 2024
@AlexJerabek
Copy link
Contributor

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?

@AlexJerabek AlexJerabek added Area: Excel Feedback on Excel content Type: doc request Request for new documentation or updates/enhancements to existing documentation and removed Needs: triage 🔍 new issue, needs PM on rotation to triage asap labels Jan 4, 2024
@ChemMitch
Copy link
Author

Thank you once again, Alex!

A follow-up question. Given this code

export async function getVocabularySheet(workbook: Excel.Workbook): Promise<Excel.Worksheet> {
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
Copy link
Contributor

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
Copy link

junaid042 commented Jan 5, 2024

@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
Copy link
Author

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

@Rick-Kirkham
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Excel Feedback on Excel content Type: doc request Request for new documentation or updates/enhancements to existing documentation
Projects
None yet
Development

No branches or pull requests

5 participants