在线智能表格中,使用AirScript Hyperlinks.Add给单元格插入超链接,求助
在在线智能表格中,我想要点击某个单元格,实现隐藏或显示某一个sheet。就想一个开关一样。
因此我给这个单元格,插入一个超链接,类型是AirScript,地址就是某一个AirScript的路径,比如:extension://airscript/V2-1wttAxrM5zu6PffGXTjelT?script_type=file,手动选择一个AirScript时,自动显示的。
现在我点击这个单元格,就自动运行了这个AirScript脚本,运行后,会把我想要设置的sheet,隐藏或取消隐藏,同时,我想把这个B2单元格超链的文本,也对应修改一下。
但使用.Hyperlinks.Add时,无法给B2单元格添加一个运行AirScript脚本的超链接。
代码如下:
function hide_sheet(targetWorkBookObj,targetSheetName){
console.log("[INFO] function: [hide_sheet] started.");
// 检查Sheet是否存在
if (!isSheetExist(targetWorkBookObj, targetSheetName)) {
MsgBox("错误:工作表【" + targetSheetName + "】不存在!");
return {
"exec_return": false,
"exec_reason": "工作表不存在",
"sheet_stat": false
};
}
let sht = targetWorkBookObj.Sheets(targetSheetName);
if (!isSheetVisible(targetWorkBookObj,targetSheetName).sheet_visible) {
sht.Visible = true;
return {
"exec_return":true,
"exec_reason": "工作表存在",
"exec_stat":true
}
}
else{
sht.Visible = false;
return {
"exec_return":true,
"exec_reason": "工作表存在",
"exec_stat":false
}
}
}
//检查sheet是否存在
function isSheetExist(targetWorkBookObj,targetSheetName)
{
//console.log("[INFO] function: [isSheetExist] started.");
//console.log("[INFO] function: [isSheetExist] check WorkBook:",targetWorkBookObj.Name);
//console.log("[INFO] function: [isSheetExist] check WorkSheet:",targetSheetName);
try {
const sht = targetWorkBookObj.Sheets(targetSheetName);
// 能正常拿到对象,说明存在
if (sht && sht.Name)
{
//console.log("[INFO] function: [isSheetExist] done. ",targetSheetName, " exists.");
return true;
}
else
{
//console.log("[INFO] function: [isSheetExist] done. ",targetSheetName, "does not exist.");
return false;
}
}
catch (err)
{
//console.error("[ERROR] function: [isSheetExist] failed. Err Msg:", err.message);
return false;
}
}
function isSheetVisible(targetWorkBookObj,targetSheetName)
{
// 检查Sheet是否存在
if (!isSheetExist(targetWorkBookObj, targetSheetName)) {
MsgBox("错误:工作表【" + targetSheetName + "】不存在!");
return {
"exec_return": false,
"exec_reason": "工作表不存在",
"sheet_visible": false
};
}
const sht = targetWorkBookObj.Sheets(targetSheetName);
return {
"exec_return": true,
"exec_reason": "工作表存在",
"sheet_visible": sht.Visible
};
}
let targetWorkBook = Application.ActiveWorkbook;
let targetWorksheet = targetWorkBook.Sheets("Main")
let targetCell = targetWorksheet.Cells.Item(1,2)
let hide_flag = hide_sheet(targetWorkBook,"rate")
if (hide_flag.exec_stat) {
targetCell.Value2 = "hide";
targetWorksheet.Hyperlinks.Add(
targetCell,
"extension://airscript/V2-1wttAxrM5zu6PffGXTjelT?script_type=file",
undefined,
undefined,
"hide"
);
}
else {
targetCell.Value2 = "unhide";
targetWorksheet.Hyperlinks.Add(
targetCell,
"extension://airscript/V2-1wttAxrM5zu6PffGXTjelT?script_type=file",
undefined,
undefined,
"unhide"
);
}
脚本运行后变成了:类型变成了链接,点击后,无法运行AirScript脚本。