已指示我创建一个与范围函数具有相同结果的函数,而无需实际使用范围函数。我有它,所以它打印升序列表,但是当我尝试使其以-1的步长打印降序列表时,它会打印一个空列表。最后我还会得到一个额外的数字,请检查下面的测试行。
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Raw Data");
var wsFeedback = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Drop Downs - Feedback");
var feedback = wsFeedback.getRange(2, 1,wsFeedback.getLastRow()-1,2).getValues();
var wsTechnical = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Drop Downs - Technical");
var technical = wsTechnical.getRange(2, 1,wsTechnical.getLastRow()-1,2).getValues();
function onEdit(e){
var activeCell = e.range;
var val = activeCell.getValue();
var r = activeCell.getRow();
var c = activeCel1.getColumn();
var wsName = activeCell.getSheet().getName();
if(wsName == "Raw Data" && c === 4 && r > 2){
if(val === ""){
ws.getRange(r, 5).clearContent();
ws.getRange(r, 5).clearDataValidations();
} else {
ws.getRange(r, 5).clearContent();
var filteredFeedback = feedback.filter(function(f){ return f[0] === val });
var listToApply = filteredFeedback.map(function(f){ return f[1] });
var cell = ws.getRange(r, 5);
applyValidationToCell(listToApply,cell);
}
}
}
function onEdit(e){
var activeCell = e.range;
var val = activeCell.getValue();
var r = activeCell.getRow();
var c = activeCell.getColumn();
var wsName = activeCell.getSheet().getName();
if(wsName == "Raw Data" && c === 6 && r > 2){
if(val === ""){
ws.getRange(r, 7).clearContent();
ws.getRange(r, 7).clearDataValidations();
} else {
ws.getRange(r, 7).clearContent();
var filteredTechnical = technical.filter(function(t){ return t[0] === val });
var listToApply = filteredTechnical.map(function(t){ return t[1] });
var cell = ws.getRange(r, 7);
applyValidationToCell(listToApply,cell);
}
}
}
function applyValidationToCell(list,cell){
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(list)
.setAllowInvalid(false)
.build();
cell.setDataValidation(rule)
}
def myRange(start = 0, stop = 0, step = 1):
number = start
number_list = []
if start == stop:
return []
while number <= stop:
number_list.append(number)
number = number + step
return number_list
print(myRange(2,4, 6))
print(myRange(2,4, 1))
print(myRange(0,5,1))
print(myRange(4,1,-1))
[2]
[2, 3, 4]
[0, 1, 2, 3, 4, 5]
[]
答案 0 :(得分:0)
使用 if 语句
def myRange(start = 0, stop = 0, step = 1):
number = start
number_list = []
if step > 0 :
while number <= stop:
number_list.append(number)
number = number + step
elif step < 0:
while number >= stop:
number_list.append(number)
number = number + step
return number_list