如何让 Appscript 识别合并来自不同电子表格行的相同值的关联数组

How to get Appscript to recognize associative array that merges same values from different spreadsheet rows

我在阅读电子表格时遇到了一些挑战:

abc@domain.com   some value
xyz@domain.com   other value
abc@domain.com   new value

我的代码到目前为止,但是当行记录显示相同的 'email' 地址时我遇到了麻烦,然后我想在 JSON 记录中连接或合并为任务数组。:

var emailObject = [];


  data.forEach(function (row) {
    var dueDate = row[9];
    var name = row[6].toString();
    var task = row[0] + ": " + row[1] + " was due on " + dueDate;
    var status = row[10];

    if ((typeof dueDate == "object" && name != "") && today.valueOf() > dueDate.valueOf() && status.toString() != "Completed" ) {
       var user = findUser(name);
       var isAdmin = user[1];
       var email = user[0];
       var varName = name.replace(' ', '_');
       if (email) {
        emailObject.push({
          email: email,
          tasks: task
        });
       }
    }
  });

输出:

[
  {
    "email": "abc@domain.com",
    "tasks": "some value"
  },
  {
    "email": "abc@domain.com",
    "tasks": "other value"
  },
  {
    "email": "xyz@domain.com",
    "tasks": "new value"
  }
]

期望的输出:

[
  {
    "email": "abc@domain.com",
    "tasks": [
      {
        "task": "some value"
      },
      {
        "task": "other value"
      }
    ]
  },
  {
    "email": "xyz@domain.com",
    "tasks": "new value"
  }
]

TIA

您可以使用 array.findIndex() 检查当前电子邮件地址是否已存在于您的电子邮件对象数组中。

示例代码:

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var data = sheet.getDataRange().getDisplayValues();

  var emailObject = [];

  data.forEach(function (row) {
    var email = row[0];
    var task = row[1];
    
    var index = emailObject.findIndex(function(item) {
      return item.email === email;
    });

    if( index > -1 ){
      //email exist, append tasks
      if(Array.isArray(emailObject[index].tasks)){
        //Push to array of tasks
        emailObject[index].tasks.push({"task": task});
      }else{
        //make Tasks key string to an array
        tmpTask = [
          {
            "task": emailObject[index].tasks
          },
          {
            "task": task
          }
        ];
        emailObject[index].tasks = tmpTask;
      }

    }else{
       //Add new key
      emailObject.push({
          email: email,
          tasks: task
        });
    }
  });

  Logger.log(emailObject);

样本Sheet:

输出:

2:53:47 AM  Notice  Execution started
2:53:49 AM  Info    [{tasks=[{task=some value}, {task=new value}, {task=any value}], email=abc@domain.com }, {tasks=other value, email=xyz@domain.com}]
2:53:50 AM  Notice  Execution completed

注:

  • 所提供的解决方案是根据您给出的所需输出而定的。尽管我觉得 task 键在您的 tasks 数组中是多余的。如果你决定只有一个 tasks 的数组没有键,你可以参考这个修改代码:

  //email exist, append tasks
  if(Array.isArray(emailObject[index].tasks)){
    //Push to array of tasks
    emailObject[index].tasks.push(task);
  }else{
    //make Tasks key string to an array
    emailObject[index].tasks = [emailObject[index].tasks, task];
  }

输出:

2:57:38 AM  Notice  Execution started
2:57:39 AM  Info    [{tasks=[some value, new value, any value], email=abc@domain.com }, {email=xyz@domain.com, tasks=other value}]
2:57:40 AM  Notice  Execution completed