UPDATE: 在同一工作表中使用下拉菜单隐藏命名范围(列)

huangapple go评论109阅读模式
英文:

UPDATE: Hide named ranges (columns) with dropdown in same sheet

问题

我正在尝试创建一个用于单元格B2中下拉选择的脚本,当选择其中一个选项时,将隐藏列表工作表中对应的列范围。

单元格B2中的下拉列表具有以下数据验证条件:
"NORMAL," "HARD," "MAX POINTS"

工作表中的以下列范围与括号中的选择相对应:
列D-K("NORMAL"),列L-S("HARD"),列T-AA("MAX POINTS")

我希望脚本能够使选择下拉选项中的一个将隐藏与其他两个下拉选项相对应的列范围(即,如果选择"HARD",它将隐藏列D-K以及列T-AA)。

我在这里做错了什么?我当然很确定有很多地方出错。

修改后的脚本: 我已经成功使每个选项在单独运行时正常工作,但每次都需要在每次运行后取消隐藏,否则将累积隐藏的内容。我已将其设置为在编辑时触发。它非常接近了,触发或者我可能需要添加一些内容以在更改选择之前重置为取消隐藏所有内容?(不确定如何做)

var ss = SpreadsheetApp.getActive();
var value1 = "NORMAL";
var value2 = "HARD";
var value3 = "MAX POINTS";
var activeSheet = ss.getActiveSheet();
var cell = activeSheet.getRange("B2").getValue();

function HideColumn() {
  if (cell == value1) {
    activeSheet.hideColumns(12, (27 - 7 + 1));
  } else if (cell == value2) {
    activeSheet.hideColumns(4, (14 - 7 + 1));
    activeSheet.hideColumns(21, (14 - 7 + 1));
  } else if (cell == value3) {
    activeSheet.hideColumns(4, (22 - 7 + 1));
  }
}

【链接】(https://docs.google.com/spreadsheets/d/1OVOxTrCto1JH02Efw9iLcUihy7LPjK1WJLgNTYU3yGY/edit?usp=sharing)

英文:

I am attempting to create a script for a dropdown selection in cell B2, that when selected for one, will hide columns corresponding two the other choices in the list sheet.

The dropdown in cell B2 is has as its data validation criteria the following list:
"NORMAL," "HARD," "MAX POINTS"

And the following column ranges in the sheet correspond to the selections in parentheses:
Columns D-K ("NORMAL"), Columns L-S ("HARD"), Columns T-AA ("MAX POINTS")

I would like the script to work such that selection of one of the dropdown choices will hide the column ranges that correspond to the two other dropdown choices (i.e., if you select "HARD" it will hide Columns D-K as well as T-AA).

What am I doing wrong here? I'm sure quite a bit of course.
link

MODIFIED SCRIPT: I got this to work properly for each when run separately, but it requires me to to unhide after each time, otherwise it compounds what is hidden. I have it set to trigger on edit. It's so close, is there something about the trigger or perhaps I need to somehow add something that resets it to unhide all before I can change the selection? (not sure how though)

var ss=SpreadsheetApp.getActive();
var value1 = "NORMAL";
var value2 = "HARD";
var value3 = "MAX POINTS";
var activeSheet = ss.getActiveSheet();
var cell = activeSheet.getRange("B2").getValue();

function HideColumn() {
  if(cell == value1) {
      activeSheet.hideColumns(12, (27-7+1));
  }
  else if(cell == value2) {
        activeSheet.hideColumns(4, (14-7+1));
        activeSheet.hideColumns(21, (14-7+1));
  }
   else if(cell == value3) {
      activeSheet.hideColumns(4, (22-7+1));
  }
} 

答案1

得分: 1

抱歉,你的请求要求只返回翻译的部分,不包含任何其他内容。以下是代码部分的翻译:

// 作为可安装的 onEdit() 触发器运行
// 监视单元格 B2
function showHideColumns(e){

  var sheetName = "今日比赛对决"
  // Logger.log(JSON.stringify(e)) // 调试

  if (e.range.columnStart == 2 && e.range.rowStart == 2 && e.range.getSheet().getName() == sheetName){
    // 正确的工作表和正确的单元格
    // Logger.log("调试:正确的工作表和正确的单元格")
  }
  else{
    // 不是正确的工作表/单元格
    // Logger.log("调试:不是正确的工作表/单元格")
    return
  }
  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName(sheetName)
  var value1 = "正常";
  var value2 = "困难";
  var value3 = "最大点数";
  var value1ColStart = 4
  var value2ColStart = 12
  var value3ColStart = 20
  var valueSetNumCols = 8
  // 列 D-K("正常"), 
  // 列 L-S("困难"), 
  // 列 T-AA("最大点数")

  var cell = e.value
  // Logger.log("调试:下拉框的值 = "+cell)

  switch (cell) {
    case value1: 
    // 正常
    // 显示正常,隐藏困难和最大点数
    // 显示所有列(包括正常)
    sheet.showColumns(value1ColStart,24)
    // 隐藏困难
    sheet.hideColumns(value2ColStart, valueSetNumCols)
    // 隐藏最大点数
    sheet.hideColumns(value3ColStart, valueSetNumCols)
    break

    case value2: 
      // 困难
      // 显示困难,隐藏正常和最大点数
      // 显示所有列
      sheet.showColumns(value1ColStart,24)
      // 隐藏正常
      sheet.hideColumns(value1ColStart, valueSetNumCols)
      // 隐藏最大点数
      sheet.hideColumns(value3ColStart, valueSetNumCols)
      break

    default:
      // 最大点数
      // 显示最大点数,隐藏正常和困难
      // 显示所有列
      sheet.showColumns(value1ColStart,24)
      // 隐藏正常
      sheet.hideColumns(value1ColStart, valueSetNumCols)
      // 隐藏困难
      sheet.hideColumns(value2ColStart, valueSetNumCols)
  }
}

希望这对你有所帮助。如果你需要更多翻译,请告诉我。

英文:

You have a dropdown in Cell B2.

  • There are three options: "Normal", "Hard", and "Max Points".
  • Depending on the value selected, you want to show the columns for the selected value and hide the columns for the other options.

The script uses the switch statement Doc ref as an alternative to an IF statement.

To run this answer:

  • copy to the Project editor,
  • create an installable onEdit() trigger. (enables the script to run when the dropdown cell is edited; and enables Event objects to be used)

// running as an installable onEdit() trigger
// watching cell B2
function showHideColumns(e){
var sheetName = "Today's Matchups"
// Logger.log(JSON.stringify(e)) // DEBUG
if (e.range.columnStart ==2 && e.range.rowStart ==2 && e.range.getSheet().getName() == sheetName){
// correct sheet and correct cell
// Logger.log("DEBUG: correct sheet and correct cell")
}
else{
// not the correct sheet/cell
// Logger.log("DEBUG: not the correct sheet/cell")
return
}
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName(sheetName)
var value1 = "NORMAL";
var value2 = "HARD";
var value3 = "MAX POINTS";
var value1ColStart = 4
var value2ColStart = 12
var value3ColStart = 20
var valueSetNumCols = 8
// Columns D-K ("NORMAL"), 
// Columns L-S ("HARD"), 
// Columns T-AA ("MAX POINTS")
var cell = e.value
// Logger.log("DEBUG: dropdown value = "+cell)
switch (cell) {
case value1: 
// NORMAL
// show NORMAL, hide Hard & Max Points
// show all columns (including NORMAL)
sheet.showColumns(value1ColStart,24)
// hide Hard
sheet.hideColumns(value2ColStart, valueSetNumCols)
// hide MaxPoints
sheet. hideColumns(value3ColStart, valueSetNumCols)
break
case value2: 
// HARD
// show HARD, hide Normal & Max Points
// show all Columns
sheet.showColumns(value1ColStart,24)
// hide Normal
sheet.hideColumns(value1ColStart, valueSetNumCols)
// hide MaxPoints
sheet. hideColumns(value3ColStart, valueSetNumCols)
break
default:
// Max Points
// show Max Points, hide Normal & Hard
// show all Columns
sheet.showColumns(value1ColStart,24)
// hide Normal
sheet.hideColumns(value1ColStart, valueSetNumCols)
// hide Hard
sheet. hideColumns(value2ColStart, valueSetNumCols)
}
}

UPDATE: ALSO SHOW/HIDE COLUMNS ON SHEET="Battle Results Output"

// runnings as an installable onEdit() trigger
// watching cell B2
// also hide columns on "Battle Results Output"
function showHideColumns(e){
var sheetName = "Today's Matchups"
// Logger.log(JSON.stringify(e)) // DEBUG
if (e.range.columnStart ==2 && e.range.rowStart ==2 && e.range.getSheet().getName() == sheetName){
// right sheet and right cell
// Logger.log("DEBUG: right sheet and right cell")
}
else{
// not the right sheet/cell
// Logger.log("DEBUG: not the right sheet/cell")
return
}
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName(sheetName)
var value1 = "NORMAL";
var value2 = "HARD";
var value3 = "MAX POINTS";
var value1ColStart = 4
var value2ColStart = 12
var value3ColStart = 20
var valueSetNumCols = 8
// Columns D-K ("NORMAL"), 
// Columns L-S ("HARD"), 
// Columns T-AA ("MAX POINTS")
/*
*  HIDE columns on Battle Results Output
*/
var battleResultsName = "Battle Results Output"
var battleSheet = ss.getSheetByName(battleResultsName)
// Value 1 = show D&E, Hide F,G&H
var value1BattleColStart = 4 // Column D
var value1BattleSetNumCols = 2
// Value 2 = show F&G, Hide D,E&H
var value2BattleColStart = 6 // Column F & G
var value2BattleSetNumCols = 2
// Value 3 = show H, Hide D&F
var value3BattleColStart = 8 // Column D
var value3BattleSetNumCols = 1
// number of columns for all results
var valueBattleShowAllCols = value1BattleSetNumCols+value2BattleSetNumCols+value3BattleSetNumCols
var cell = e.value
// Logger.log("DEBUG: dropdown value = "+cell)
switch (cell) {
case value1: 
// NORMAL
// show NORMAL, hide Hard & Max Points
// show Normal
sheet.showColumns(value1ColStart,24)
// hide Hard
sheet.hideColumns(value2ColStart, valueSetNumCols)
// hide MaxPoints
sheet.hideColumns(value3ColStart, valueSetNumCols)
/*
*  HIDE columns from Battle Results
*/
// show all including Normal
battleSheet.showColumns(value1BattleColStart,valueBattleShowAllCols)
// hide Hard
battleSheet.hideColumns(value2BattleColStart,value2BattleSetNumCols)
// hide MaxPoints
battleSheet.hideColumns(value3BattleColStart,value3BattleSetNumCols)
break
case value2: 
// HARD
// show HARD, hide Normal & Max Points
// show Normal
sheet.showColumns(value1ColStart,24)
// hide Normal
sheet.hideColumns(value1ColStart, valueSetNumCols)
// hide MaxPoints
sheet.hideColumns(value3ColStart, valueSetNumCols)
/*
*  HIDE columns from Battle Results
*/
// show all including Normal
battleSheet.showColumns(value1BattleColStart,valueBattleShowAllCols)
// hide Normal
battleSheet.hideColumns(value1BattleColStart,value1BattleSetNumCols)
// hide MaxPoints
battleSheet.hideColumns(value3BattleColStart,value3BattleSetNumCols)
break
default:
// Max Points
// show Max Points, hide Normal & Hard
// show Normal
sheet.showColumns(value1ColStart,24)
// hide Normal
sheet.hideColumns(value1ColStart, valueSetNumCols)
// hide Hard
sheet. hideColumns(value2ColStart, valueSetNumCols)
/*
*  HIDE columns from Battle Results
*/
// show all including Normal
battleSheet.showColumns(value1BattleColStart,valueBattleShowAllCols)
// hide Normal
battleSheet.hideColumns(value1BattleColStart,value1BattleSetNumCols)
// hide Hard
battleSheet.hideColumns(value2BattleColStart,value2BattleSetNumCols)
}
// Logger.log("DEBUG: Selection: "+cell+" the end")
}

huangapple
  • 本文由 发表于 2023年2月18日 01:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定