添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Forum Discussion

ArchieSmith
Copper Contributor
Dec 22, 2022
Solved

Office Script If Statement not working

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:

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.

  • 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');