更改自定义函数返回的日期格式 - google 工作表/应用程序脚本

changing date format returned in custom function - google sheets/ appscript

我一直在使用这个我从之前的问题

调整过的脚本并且它一直运行良好,除了我开始在一个新的 sheet 并且日期格式现在显示为
"Tue May 03 2022 00:00:00 GMT-0800 (GMT-08:00) - 381.1
2022 年 5 月 4 日星期三 00:00:00 GMT-0800 (GMT-08:00) - 70.7"
而不是之前显示的 -
"5/3/22 - 381.1
5/4/22 - 70.7"
我确定这与源数据单元格中的日期格式有关,但我无法更改它们,因为它们正在拉动来自其他文档并比较数据。
这是我进行的测试 sheet:https://docs.google.com/spreadsheets/d/1etBO4hsrvmi_rw6F_Bw7T4DgLzHvfueIHdKa6wezWjA/edit#gid=1539515971
这是脚本,该函数在 sheet "Master":

的 A2 中调用
function createMaster(source) {
  const ss = SpreadsheetApp.getActive();
  const sourceSheet = ss.getSheetByName(source);
  const sourceArray = sourceSheet.getRange(2,1, sourceSheet.getLastRow(), 14); // Get source data
  const sourceValues = sourceArray.getValues()
  const groups = sourceValues.map(row => { // Get unique combinations
    return JSON.stringify([row[0]].concat(row.slice(2,6)).concat(row.slice(9,10)));
  }).reverse().filter((e, i, a) => a.indexOf(e, i + 1) === -1)
    .reverse().map(JSON.parse);
  let groupedData = groups.map(group => {
    const groupRows = sourceValues.filter(row => { // Get matching rows
      return JSON.stringify([row[0]].concat(row.slice(2,6)).concat(row.slice(9,10))) === JSON.stringify(group);
    });
    return groupRows.reduce((acc, current) => { // Adding the values from same combination
      const date = acc[1] === "" ? current[1] + " - " + current[8]: acc[1] + "\n" + current[1] + " - " + current[8];
      const caseType = acc[6] === current[6] ? acc[6] : acc[6] + "\n" + String(current[6]);
      const freshFrozen = acc[5] === current[5] ? acc[5] : acc[5] + "\n" + String(current[5]);
      const aggregate = acc[10] === current[10] ? acc[10] : acc[10] + "\n" + String(current[10]);
      const location = acc[13] === "" ? current[13] : acc[13] + "\n" + current[13];
      const pallet = acc[11] === current[11] ? acc[11] : acc[11] + "\n" + String(current[11]);
      const unit = (Number(acc[7]) + Number(current[7]) > 0) ? (Number(acc[7]) + Number(current[7])) : 1;
      const weight = Number(acc[8]) + Number(current[8]);
      const comments = acc[12] + "\n" + current[12];
      return [group[0], date, ...group.slice(1,4), freshFrozen, caseType, unit, weight, group[5], aggregate, pallet, comments, location];
    }, Array(14).fill(""));
  });
  groupedData.forEach(row => { // Removing duplicate dates, case types, locations
    row[1] = [...new Set(row[1].split("\n"))].join("\n");
    row[5] = [...new Set(row[5].split("\n"))].join("\n");
    row[6] = [...new Set(row[6].split("\n"))].join("\n");
    row[11] = [...new Set(row[11].split("\n"))].join("\n");
    row[12] = [...new Set(row[12].split("\n"))].join("\n");
    row[13] = [...new Set(row[13].split("\n"))].join("\n");
  });
  return groupedData;
}

我相信你的目标如下。

  • 您想在脚本中 5/3/22 - 381.1\n5/4/22 - 70.7 而不是 Tue May 03 2022 00:00:00 GMT-0800 (GMT-08:00) - 381.1\nWed May 04 2022 00:00:00 GMT-0800 (GMT-08:00) - 70.7

如果我的理解是正确的,下面的修改怎么样?

发件人:

return groupRows.reduce((acc, current) => { // Adding the values from same combination
  const date = acc[1] === "" ? current[1] + " - " + current[8]: acc[1] + "\n" + current[1] + " - " + current[8];

收件人:

return groupRows.reduce((acc, current) => { // Adding the values from same combination

  current[1] = current[1] instanceof Date ? Utilities.formatDate(current[1], ss.getSpreadsheetTimeZone(), "dd/MM/yy") : current[1]; // Added

  const date = acc[1] === "" ? current[1] + " - " + current[8]: acc[1] + "\n" + current[1] + " - " + current[8];

测试:

将此修改用于您的脚本和您提供的电子表格时,将获得以下结果。

发件人:

收件人:

注:

  • 根据你的问题,我使用 MM/dd/yy 作为日期格式。如果要修改为dd/MM/yy,请自行修改

参考:

试试下面的代码:

function createMaster(source) {
  const ss = SpreadsheetApp.getActive();
  const sourceSheet = ss.getSheetByName(source);
  const sourceArray = sourceSheet.getRange(2, 1, sourceSheet.getLastRow(), 14); // Get source data
  const sourceValues = sourceArray.getValues()
  const groups = sourceValues.map(row => { // Get unique combinations
    return JSON.stringify([row[0]].concat(row.slice(2, 6)).concat(row.slice(9, 10)));
  }).reverse().filter((e, i, a) => a.indexOf(e, i + 1) === -1)
    .reverse().map(JSON.parse);
  let groupedData = groups.map(group => {
    const groupRows = sourceValues.filter(row => { // Get matching rows
      return JSON.stringify([row[0]].concat(row.slice(2, 6)).concat(row.slice(9, 10))) === JSON.stringify(group);
    });
    return groupRows.reduce((acc, current) => { // Adding the values from same combination
    //Added codes to convert object to string and change date format *** START
      if (typeof(current[1]) != "string"){
        var dateString = current[1].toDateString();
        var formattedDate = Utilities.formatDate(new Date(dateString), "SAST", "MM/dd/YY");
      }
      var date = acc[1] === "" ? formattedDate + " - " + current[8] : acc[1] + "\n" + formattedDate + " - " + current[8];
      if (date == "undefined - "){
        date = " - ";
      }
      //Added codes to convert object to string and change date format ***END
      const caseType = acc[6] === current[6] ? acc[6] : acc[6] + "\n" + String(current[6]);
      const freshFrozen = acc[5] === current[5] ? acc[5] : acc[5] + "\n" + String(current[5]);
      const aggregate = acc[10] === current[10] ? acc[10] : acc[10] + "\n" + String(current[10]);
      const location = acc[13] === "" ? current[13] : acc[13] + "\n" + current[13];
      const pallet = acc[11] === current[11] ? acc[11] : acc[11] + "\n" + String(current[11]);
      const unit = (Number(acc[7]) + Number(current[7]) > 0) ? (Number(acc[7]) + Number(current[7])) : 1;
      const weight = Number(acc[8]) + Number(current[8]);
      const comments = acc[12] + "\n" + current[12];

      return [group[0], date, ...group.slice(1, 4), freshFrozen, caseType, unit, weight, group[5], aggregate, pallet, comments, location];
    }, Array(14).fill(""));
  });
  groupedData.forEach(row => { // Removing duplicate dates, case types, locations
    row[1] = [...new Set(row[1].split("\n"))].join("\n");
    row[5] = [...new Set(row[5].split("\n"))].join("\n");
    row[6] = [...new Set(row[6].split("\n"))].join("\n");
    row[11] = [...new Set(row[11].split("\n"))].join("\n");
    row[12] = [...new Set(row[12].split("\n"))].join("\n");
    row[13] = [...new Set(row[13].split("\n"))].join("\n");
  });
  return groupedData;
}

我稍微调整了你的代码。目前您的日期是一个对象,因此必须将其转换为日期字符串,然后应用您想要的格式 MM/dd/YY,这就是我添加的代码所做的。

结果如下:

参考: