Set DataValidation in a list of columns at once

I’m trying to rewrite some old VBA code to Office Script.
One of the steps is to set data validation on several columns.
With the script I am able to loop all columns of a named table.
So my idea was to use a switch on the column names to set the related data validation.
So far so good – but: all defined rules set in the ForEach loop are somehow not done when calling the setRule() but all columns do have afterwards the last defined rule (here: the one from column name “Lagerstatus”.
Does anyone knows how to solve this problem?
Thanks in advance!
Best regeards,
Pascal

Code snippet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> let sbOrderTable = workbook.getTable("mainOrderTable");
const errorAlertStype = ExcelScript.DataValidationAlertStyle.stop;
sbOrderTable.getColumns().forEach(column => {
let columnName = column.getName();
let errorMessage = "Nur Einträge aus dem PullDown-Menü sind zulässig";
let dataValidation: ExcelScript.DataValidation;
dataValidation = column.getRange().getDataValidation();
dataValidation.clear();
dataValidation.setIgnoreBlanks(true);
dataValidation.setPrompt({ showPrompt: false, title: "", message: "" });
dataValidation.setErrorAlert({ showAlert: true, title: columnName, message: errorMessage, style: errorAlertStype });
switch (columnName) {
case "Lieferant":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } });
case "Hersteller":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } }); 1
case "Ersatzteil":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } });
case "Bestell Index":
//errorMessage = "Ungültiger Bestell-Index gemäss Referenz";
dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } });
case "Bestellt von":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } });
case "Bestellstatus":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
case "Lagerstatus":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
}
});
</code>
<code> let sbOrderTable = workbook.getTable("mainOrderTable"); const errorAlertStype = ExcelScript.DataValidationAlertStyle.stop; sbOrderTable.getColumns().forEach(column => { let columnName = column.getName(); let errorMessage = "Nur Einträge aus dem PullDown-Menü sind zulässig"; let dataValidation: ExcelScript.DataValidation; dataValidation = column.getRange().getDataValidation(); dataValidation.clear(); dataValidation.setIgnoreBlanks(true); dataValidation.setPrompt({ showPrompt: false, title: "", message: "" }); dataValidation.setErrorAlert({ showAlert: true, title: columnName, message: errorMessage, style: errorAlertStype }); switch (columnName) { case "Lieferant": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } }); case "Hersteller": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } }); 1 case "Ersatzteil": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } }); case "Bestell Index": //errorMessage = "Ungültiger Bestell-Index gemäss Referenz"; dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } }); case "Bestellt von": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } }); case "Bestellstatus": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } }); case "Lagerstatus": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } }); } }); </code>
    let sbOrderTable = workbook.getTable("mainOrderTable");
    const errorAlertStype = ExcelScript.DataValidationAlertStyle.stop;

    sbOrderTable.getColumns().forEach(column => {
        let columnName = column.getName();
        let errorMessage = "Nur Einträge aus dem PullDown-Menü sind zulässig";
        let dataValidation: ExcelScript.DataValidation;
        
        dataValidation = column.getRange().getDataValidation();
        dataValidation.clear();
        dataValidation.setIgnoreBlanks(true);
        dataValidation.setPrompt({ showPrompt: false, title: "", message: "" });
        dataValidation.setErrorAlert({ showAlert: true, title: columnName, message: errorMessage, style: errorAlertStype });

        switch (columnName) {
            case "Lieferant":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } });
            case "Hersteller":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } }); 1
            case "Ersatzteil":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } });
            case "Bestell Index":
                //errorMessage = "Ungültiger Bestell-Index gemäss Referenz";
                dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } });
            case "Bestellt von":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } });
            case "Bestellstatus":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
            case "Lagerstatus":
                dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
        }
    });

Pls add break for each case clause.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>switch (columnName) {
case "Lieferant":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } });
break;
case "Hersteller":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } });
break;
case "Ersatzteil":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } });
break;
case "Bestell Index":
//errorMessage = "Ungültiger Bestell-Index gemäss Referenz";
dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } });
break;
case "Bestellt von":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } });
break;
case "Bestellstatus":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
break;
case "Lagerstatus":
dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
break;
default:
// if non-macthing above
}
</code>
<code>switch (columnName) { case "Lieferant": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } }); break; case "Hersteller": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } }); break; case "Ersatzteil": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } }); break; case "Bestell Index": //errorMessage = "Ungültiger Bestell-Index gemäss Referenz"; dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } }); break; case "Bestellt von": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } }); break; case "Bestellstatus": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } }); break; case "Lagerstatus": dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } }); break; default: // if non-macthing above } </code>
switch (columnName) {
        case "Lieferant":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$D$6:$D$1048576" } });
            break;
        case "Hersteller":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$B$6:$B$1048576" } });
            break;
        case "Ersatzteil":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$A$6:$A$7" } });
            break;
        case "Bestell Index":
            //errorMessage = "Ungültiger Bestell-Index gemäss Referenz";
            dataValidation.setRule({ list: { inCellDropDown: false, source: "=REF_IDX!$A$6:$A$1048576" } });
            break;
        case "Bestellt von":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF_SYNC!$A$6:$A$1048576" } });
            break;
        case "Bestellstatus":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
            break;
        case "Lagerstatus":
            dataValidation.setRule({ list: { inCellDropDown: true, source: "=REF!$B$6:$B$1048576" } });
            break;
        default:
            // if non-macthing above
    }

A simple demo of switch / case / break.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const fruit = "banana";
switch (fruit) {
case "apple":
console.log("Apple selected");
case "banana":
console.log("Banana selected");
case "grape":
console.log("Grape selected");
default:
console.log("Unknown fruit");
}
</code>
<code>const fruit = "banana"; switch (fruit) { case "apple": console.log("Apple selected"); case "banana": console.log("Banana selected"); case "grape": console.log("Grape selected"); default: console.log("Unknown fruit"); } </code>
const fruit = "banana";

switch (fruit) {
  case "apple":
    console.log("Apple selected");
  case "banana":
    console.log("Banana selected");
  case "grape":
    console.log("Grape selected");
  default:
    console.log("Unknown fruit");
}

You might expect the output to be Banana selected. However, the actual output is as follows. Since there’s no break, after the “banana” case matches, the code continues executing the “grape” and “default” cases as well, causing unintended behavior.

Output in console:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Banana selected
Grape selected
Unknown fruit
</code>
<code>Banana selected Grape selected Unknown fruit </code>
Banana selected
Grape selected
Unknown fruit

Back to your script, there are 7 case clauses without break. For the first column (“Lieferant”), the script applies the data validation rules 7 times, with the last rule overwriting the previous ones.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật