如果没有示例和上下文,这很难标题。来...
我有一个Google应用程序脚本,该脚本搜索一列学生ID(compiledDATA表格上的列A ),然后在同一行的B列中设置一个值(奖励)。这对于单个学生ID来说效果很好,但是我需要脚本来循环并为 GroupAwardIDs 范围内的所有学生设置相同的奖励值,该范围位于名为 Group的单独工作表上奖项。
这是我的示例电子表格的link。
要设置的值不是连续的,在实际使用中一次可能要设置上千个。
如何在不遇到配额问题的情况下快速有效地实现这一目标?
这是脚本(请原谅所有注释,这有助于我跟踪):
function AwardGroup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var StarLog = sheet.getSheetByName("compiledDATA");
var GroupAward = sheet.getRangeByName("GroupAward").getValue();
var GroupAwardIDs = sheet.getRangeByName("GroupAwardIDs").getValue(); // THESE ARE THE IDS OF STUDENTS WHO WILL RECEIVE THE AWARD. HOW DO SET VALUES FOR ALL AND ONLY THESE IDS?
var IDs = sheet.getRangeByName("StudentIDs").getValues(); // all of the student IDs
for (var i = 0; i < IDs.length; i++) {
if (IDs[i] == "123461") { // THIS WORKS IF HARDCODE A SINGLE ID
var rowNumber = i+3; // find row and add 3 to compensate for GroupAward range staring at row 3
var StarLogCurrent = StarLog.getRange("B"+rowNumber).getValue(); // locates students award log cell using A1 notation
var appendAward = GroupAward.concat(StarLogCurrent); // prepends new award to previous awards
StarLog.getRange("B"+rowNumber).setValue(appendAward); //write new star log
}
}
}
答案 0 :(得分:1)
您要在学生ID(“ compiledDATA!A3:A1000”)和GroupAwardIDs(“'Group' Awards !! B7:B1002“)相同。如果我的理解是正确的,那么该修改如何?我认为针对您的情况有几种解决方案。因此,请将此视为其中之一。
请进行如下修改。
来自:var GroupAwardIDs = sheet.getRangeByName("GroupAwardIDs").getValue(); // THESE ARE THE IDS OF STUDENTS WHO WILL RECEIVE THE AWARD. HOW DO SET VALUES FOR ALL AND ONLY THESE IDS?
var IDs = sheet.getRangeByName("StudentIDs").getValues(); // all of the student IDs
for (var i = 0; i < IDs.length; i++) {
if (IDs[i] == "123461") { // THIS WORKS IF HARDCODE A SINGLE ID
var rowNumber = i+3; // find row and add 3 to compensate for GroupAward range staring at row 3
var StarLogCurrent = StarLog.getRange("B"+rowNumber).getValue(); // locates students award log cell using A1 notation
var appendAward = GroupAward.concat(StarLogCurrent); // prepends new award to previous awards
StarLog.getRange("B"+rowNumber).setValue(appendAward); //write new star log
}
}
至 :
var GroupAwardIDs = sheet.getRangeByName("GroupAwardIDs").getValues(); // Modified
var IDs = sheet.getRangeByName("StudentIDs").getValues();
// I modified below script.
GroupAwardIDs = GroupAwardIDs.filter(String);
var res = IDs.map(function(e){
return GroupAwardIDs.filter(function(f){
return f[0] == e[0]
}).length > 0 ? [GroupAward] : [""];
});
sheet.getRange("compiledDATA!B3:B1000").setValues(res);
如果我误解了您的问题,请告诉我。我想修改它。
您想将GroupAward添加到B列的原始值。我知道您想要这样做。如果我的理解正确,请修改如下。在此示例中,我使用", "
作为分隔符。
var GroupAwardIDs = sheet.getRangeByName("GroupAwardIDs").getValues(); // Modified
var IDs = sheet.getRangeByName("StudentIDs").getValues();
// I modified below script.
var columnB = sheet.getRange("compiledDATA!B3:B1000");
var valColB = columnB.getValues();
GroupAwardIDs = GroupAwardIDs.filter(String);
var res = IDs.map(function(e, i){
return GroupAwardIDs.filter(function(f){
return f[0] == e[0]
}).length > 0 ? [valColB[i][0] ? GroupAward + ", " + valColB[i][0] : GroupAward] : [valColB[i][0]]; // Modified
});
columnB.setValues(res);