SOLVED

Office Script If Statement not working

Occasional Contributor

Hi everyone!

 

Apologies for my likely simple problem, I have little experience with coding but I'm trying my best to learn how to automate my Excel spreadsheets.

 

My if statement doesn't seem to want to work in office script the way I want it to and I cant work out why.

 

I want it to simply run through all the spreadsheets and check which of those have an X in cell A4 and (to start with) output yes or no in the console log but it keeps bugging.

 

Here's the code I've written so far:

ArchieSmith_0-1671706183327.png

It's outputting 14 lines of yes at the moment when only 3 sheets have the X in the cell.

I would be grateful if someone could point me in the right direction, I presume this is a pretty simple fix but I cant work it out from reading around on the web.

 

Thanks, 

 

Archie.

 

5 Replies

Hi @ArchieSmith 

 

I'm not an Office Script dev. + don't have access to the language with my subscription so no guarantee at all... What happens if you replace line #8 with:

 

if (rangeval = 'X'){

 

Hi @L z. 

Unfortunately it does the same thing, 14 'yes'es.

It's an odd language that takes a lot from TypeScript and JavaScript from what I can work out.

Thanks for the help!

best response confirmed by ArchieSmith (Occasional Contributor)
Solution

@ArchieSmith 

After a bit of reading could you try the following:

function main(workbook: ExcelScript.Workbook) {
  let wsArr = workbook.getWorksheets();
  wsArr.forEach(ws => {
    let range = ws.getRange("A4");
    let rangeval = range.getValue();
    if (rangeval == 'X') {
        console.log('Yes');
      } else {
        console.log('No');
      }
    }
  )
}
Thank you !
This works the intended way and will be a good place to start my attempt at automating.

Welcome @ArchieSmith & Thanks for providing feedback

+ Forgot to post: TypeScript Operators