如何用Google Script批量设置Google Sheet所有工作表的标签颜色
批量设置Google Sheet所有工作表标签颜色的实现方案
你不需要额外提取工作表名称再逐一查询,getSheets() 已经直接返回当前表格所有工作表的实例数组,直接遍历该数组调用标签颜色设置方法即可。
基础版(统一设置所有工作表标签颜色)
function setAllSheetTabColor() { // 获取当前激活的电子表格实例 const ss = SpreadsheetApp.getActiveSpreadsheet(); // 获取所有工作表对象 const allSheets = ss.getSheets(); // 自定义目标颜色的6位hex码,不需要加#前缀,示例为浅红色 const targetColor = "f4c7c3"; // 遍历所有工作表设置颜色 for (let i = 0; i < allSheets.length; i++) { allSheets[i].setTabColor(targetColor); } }
进阶版(支持排除指定工作表)
如果需要保留部分工作表的原有标签颜色,可以在循环中加入名称判断:
function setAllSheetTabColorWithExclude() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const allSheets = ss.getSheets(); const targetColor = "f4c7c3"; // 填写需要排除的工作表名称,可自行增减 const excludeList = ["汇总表", "说明页"]; for (let i = 0; i < allSheets.length; i++) { const currentSheet = allSheets[i]; if (!excludeList.includes(currentSheet.getName())) { currentSheet.setTabColor(targetColor); } } }
使用说明
- 将代码复制到Google Sheet顶部菜单「扩展程序 > Apps Script」的代码编辑器中,保存后运行对应函数即可,首次运行需要按提示授权相关权限。
- 如果需要清空所有工作表的标签颜色,只需把
setTabColor的参数改为null即可:allSheets[i].setTabColor(null)
内容的提问来源于stack exchange,提问作者questions4bizness
相关产品推荐
相关产品推荐

