将单元格与数组进行比较,并将 return 应用脚本中的值进行比较

Compare cell to an array and return a value in App script

我正在尝试创建一个应用程序脚本来将单元格值与数组进行比较,其中 D 列具有值(task1、task2...task6),每个任务都有 1 级或 2 级,因此如果 D2 列具有值“task1”然后它是 L1 并且该值设置在 O 列的相应行中,似乎我可能在途中犯了几个错误,因为我没有得到我想要的结果,任何帮助都会很大赞赏。

function fillValues() {
var spreadsheet = SpreadsheetApp.openById().getSheetByName()
var task = spreadsheet.getRange("D2:D" + spreadsheet.getLastRow())
var taskLevel = spreadsheet.getRange("O2:O" + spreadsheet.getLastRow())
var newRange = []
var newFillValue

task.getValues().map(function(value) {

var L1Tasks = ["task1", "task2", "task3"];
var L2Tasks = ["task4","task5", "task6"];
for (var i = 0; i < L1Tasks.length; i++){ 
if (value[0]==L1Tasks[i]) { 
newFillValue="L1" 
newRange.push([newFillValue]) }
else { newFillValue="L2" newRange.push([newFillValue]) } 
} 
}) 
taskLevel.setValues(newRange) 
}

我还没有测试过。只是一个猜测。大概应该是这样的:

function fillValues() {
    var spreadsheet = SpreadsheetApp.openById('###').getSheetByName('xxx'); // <--- your ID and sheet name should be here
    var task = spreadsheet.getRange("D2:D" + spreadsheet.getLastRow());
    var taskLevel = spreadsheet.getRange("O2:O" + spreadsheet.getLastRow());
    var newRange = taskLevel.getValues(); // <-- fill the array with values from the column
    var newFillValue;

    task.getValues().forEach((value,v) => { // <-- the counter 'v'

        var L1Tasks = ["task1", "task2", "task3"];
        // var L2Tasks = ["task4", "task5", "task6"]; // <-- doesn't need?

        for (var i = 0; i < L1Tasks.length; i++) {
            if (value[0] == L1Tasks[i]) {
                newFillValue = "L1";
                newRange[v] = [newFillValue]; // <-- assign the value
            }
            else {
                newFillValue = "L2";
                newRange[v] = [newFillValue]; // <-- assign the value
            }
        }
    })

    taskLevel.setValues(newRange);
}

可以做得更短。例如,您可以使用 if (L1Tasks.includes(value[0])) 而不是冗长的循环等

简化版在这里:

function fillValues() {
    var sheet = SpreadsheetApp.openById('###').getSheetByName('xxx');  // <--- paste your ID and sheet name
    var tasks = sheet.getRange('D2:D' + sheet.getLastRow()).getValues().flat();
    var levels_range = sheet.getRange('O2:O' + sheet.getLastRow());
    var levels = levels_range.getValues();
    var L1Tasks = ['task1', 'task2', 'task3'];

    for (var row in tasks) {
        levels[row] = L1Tasks.includes(tasks[row]) ? ['L1'] : ['L2'];
    }

    levels_range.setValues(levels);
}

更新

这是 D 列中包含逗号的代码变体:

function fillValues() {
  // var sheet = SpreadsheetApp.openById('###').getSheetByName('xxx');  // <--- paste your ID and sheet name
  var sheet = SpreadsheetApp.getActiveSheet()
  var tasks = sheet.getRange('D2:D' + sheet.getLastRow()).getValues().flat();
  var levels_range = sheet.getRange('O2:O' + sheet.getLastRow());
  var levels = levels_range.getValues();
  var L1Tasks = ['task1', 'task2', 'task3'];

  for (var row in tasks) {
    var sub_tasks = tasks[row].replace(/,\s+/g,',').split(',');
    for (var task of sub_tasks) {
      if (L1Tasks.includes(task)) {
        levels[row] = ['L1']; break;
      } else { 
        levels[row] = ['L2'];
      }
    }
  }

  levels_range.setValues(levels);
}

更新 2

如果您想为不仅包含 'L1' 任务的单元分配 'L2',您可以这样更改内部循环:

    for (var task of sub_tasks) {
      if (!L1Tasks.includes(task)) {
        levels[row] = ['L2']; break;
      } else { 
        levels[row] = ['L1'];
      }
    }

或者(相同算法的缩写):

  for (var row in tasks) {
    var sub_tasks = tasks[row].replace(/,\s+/g,',').split(',');
    levels[row] = ['L1'];
    if (sub_tasks.some(x => !L1Tasks.includes(x))) levels[row] = ['L2'];    
  }

或者(甚至更短),以防万一:

  for (var row in tasks) {
    var sub_tasks = tasks[row].replace(/,\s+/g,',').split(',');
    levels[row] = sub_tasks.every(x => L1Tasks.includes(x)) ? ['L1'] : ['L2'];
  }