乐于分享
好东西不私藏

WPS JSP宏:一键批量修改工作表名称

WPS JSP宏:一键批量修改工作表名称

/** * 批量重命名工作表(通用) * @param {stringaction  - "prefix" / "suffix" / "replace" * @param {stringstr1    - 前缀/后缀,或替换时被替换的旧字符 * @param {stringstr2    - (仅 replace 时)新字符,可选 * @param {Array}  indices - 工作表序号数组(从1开始),省略则处理全部 */function renameSheets(action, str1, str2, indices) {  if (!action) return;  if (!str1 && action !== "replace") str1 = "";  if (action === "replace" && str2 === undefined) str2 = "";  let sheetsToProcess = [];  if (indices && indices.length > 0) {    sheetsToProcess = indices;  } else {    for (let i = 1; i <= Sheets.Count; i++) {      sheetsToProcess.push(i);    }  }  for (let idx of sheetsToProcess) {    try {      let sheet = Sheets(idx);      let oldName = sheet.Name;      let newName;      switch (action) {        case "prefix":          newName = str1 + oldName;          break;        case "suffix":          newName = oldName + str1;          break;        case "replace":          newName = oldName.split(str1).join(str2);          break;        default:          throw new Error("不支持的操作类型:" + action);      }      if (newName === "") {        console.warn("工作表 '" + oldName + "' 重命名后为空,已跳过");        continue;      }      if (sheetNameExists(newName)) {        console.warn("工作表名 '" + newName + "' 已存在,跳过 '" + oldName + "'");        continue;      }      sheet.Name = newName;      console.log("已重命名:'" + oldName + "' -> '" + newName + "'");    } catch (e) {      console.error("处理工作表 " + idx + " 时出错:" + e.message);    }  }}function sheetNameExists(name) {  for (let i = 1; i <= Sheets.Count; i++) {    if (Sheets(i).Name === name) return true;  }  return false;}// ===================== 主宏:弹出菜单选择操作 =====================function 批量重命名工作表_菜单选择() {  let choice = Application.InputBox(    "请选择操作:\n1 - 添加前缀\n2 - 添加后缀\n3 - 替换字符\n\n请输入数字 1、2 或 3:",    "批量重命名工作表",    ""  );  // 检测取消(WPS InputBox 取消返回 false)  if (choice === false || choice == null) {    // 静默退出,不弹出任何消息,直接返回当前工作表    return;  }  choice = choice.trim(); // 此时 choice 一定是字符串  if (choice === "1") {    let prefix = Application.InputBox("请输入要添加的前缀:""添加前缀""");    if (prefix === false || prefix == null) {      return// 取消则退出    }    if (prefix.trim() === "") {      MsgBox("未输入前缀,操作取消。"0"提示");      return;    }    renameSheets("prefix", prefix.trim());    MsgBox("前缀添加完成!"0"完成");  } else if (choice === "2") {    let suffix = Application.InputBox("请输入要添加的后缀:""添加后缀""");    if (suffix === false || suffix == null) {      return;    }    if (suffix.trim() === "") {      MsgBox("未输入后缀,操作取消。"0"提示");      return;    }    renameSheets("suffix", suffix.trim());    MsgBox("后缀添加完成!"0"完成");  } else if (choice === "3") {    let oldStr = Application.InputBox("请输入要替换的字符:""替换字符""");    if (oldStr === false || oldStr == null) {      return;    }    // 允许旧字符为空字符串(表示删除字符),所以不强制非空    let newStr = Application.InputBox("请输入新的字符:""替换字符""");    if (newStr === false || newStr == null) {      return;    }    // 替换操作允许旧字符为空(此时相当于在开头插入新字符?但这里按原逻辑执行)    renameSheets("replace", oldStr, newStr);    MsgBox("替换完成!"0"完成");  } else {    MsgBox("输入无效,请输入 1、2 或 3。"0"错误");  }}
需要源码文件? 关注后回复”260622″,获取完整 示例文件下载链接。