我完全不熟悉编码(受过培训的会计师),我正在尝试在工作表中开发一个循环,该循环基于答案,隐藏或显示另一工作表中存在的一系列数据。我现在已经手动构建了它,但是它太慢了,所以我希望我能得到一些关于如何使用循环构建相同函数的建议。任何帮助将不胜感激。下面的示例代码...
//provides code on how to hide or show rows depending on a cell value
function HideRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var areas = ss.getSheetByName("areas");
var Budget = ss.getSheetByName("Budget by Month");
//REVENUE SECTION
//provides code on hiding tuition section
var tuition = areas.getRange('B7').getValue();
if (tuition == "no"){
Budget.hideRows(4,6);}
else {
Budget.showRows(4,6);
}
//provides code on hiding financial aid section
var aid =areas.getRange('B8').getValue();
if (aid == "no"){
Budget.hideRows(10,8);}
else {
Budget.showRows(10,8);
}
//provides code on hiding student fees section
var fees =areas.getRange('B9').getValue();
if (fees == "no"){
Budget.hideRows(18,16);
}
else {
Budget.showRows(18,16);
}
}
最佳答案
创建循环并不容易,因为 rowStart 和 rowEnd 的值并不遵循看起来的结构。但我设法使它更加紧凑和可读;
function HideRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var areas = ss.getSheetByName("areas");
var Budget = ss.getSheetByName("Budget by Month");
//REVENUE SECTION
var json = JSON.parse(''); //use my json here
json.forEach(function(item) {
if (areas.getRange(item.range).getValue() == "no"){
Budget.hideRows(item.startRow, item.endRow);
}
else {
Budget.showRows(item.startRow, item.endRow);
}
});
}
可以创建一个JSON对象来存储这些变量,这样就是一个纯循环逻辑;
[{
“范围”:“B7”,
“开始行”:4,
“结束行”:6
}, {
“范围”:“B8”,
“开始行”:10,
“结束行”:8
}, {
“范围”:“B9”,
“开始行”:18,
“endRow”:16
}]
关于javascript - 遇到循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35493890/