Created
June 6, 2019 09:31
-
-
Save Narshe1412/77afbe463eacf37d422a611268425e5c to your computer and use it in GitHub Desktop.
SpreadJS: Create List Validator from Array including empty option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getListValidatorFromArray = (spread: GC.Spread.Sheets.Workbook, data: any[]) => { | |
// saving validation list values in a hidden sheet | |
spread.addSheet(spread.getSheetCount()); | |
const sheet = spread.getSheet(spread.getSheetCount() - 1); | |
sheet.visible(false); | |
for (let i = 0; i < data.length; i++) { | |
sheet.setValue(i, 0, data[i]); | |
} | |
// create validator based on the values | |
const dv = GC.Spread.Sheets.DataValidation.createFormulaListValidator( | |
'=' + sheet.name() + '!$A$1:' + sheet.name() + '!$A$' + data.length | |
); | |
return dv; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full story: https://stackoverflow.com/q/56040022/5866637
Note: This creates an extra sheet for each validator you create. Makes sure you reuse them as much as possible (i.e. assigning it to a variable when it's created, and reusing the variable for other columns/rows that use the same one).