I want to block the end user from setting a given style (more precisely, it’s setting any style other than font weight: bold/normal) while still being allowed to set under the hood via setStyle().
Can return false from the beforeAction and accomplish the blocking. But that would also block setStyle() for the same style and cell.
I saw both col and row are undefined for setStyle() which was promising, but that’s also the case if the end user happens to select the whole sheet.
If I pass a special fake style to indicate the programmatic source, that seems to be a workaround; even a JS Symbol works. But is there a better way?
Hello,
You can handle this by adding a simple internal flag that marks programmatic style updates. When the flag is active, all style changes are allowed. When it’s not active, the change comes from the end user, so you block everything except the allowed font-weight values:
let internalStyleUpdate = false;
function applyProgramStyle() {
internalStyleUpdate = true;
spreadsheet.setStyle("A1", { textDecoration: "underline" });
internalStyleUpdate = false;
}
spreadsheet.events.on("beforeAction", (actionName, actionConfig) => {
if (internalStyleUpdate) {
return;
}
const value = actionConfig.val?.["font-weight"];
if (value === "bold" || value === "") {
return;
}
return false;
});
Example: DHTMLX Snippet Tool