如何使用数据创建摘要(Google 表格)

Posted

技术标签:

【中文标题】如何使用数据创建摘要(Google 表格)【英文标题】:How to Create the Summary using the Data (Google Sheets) 【发布时间】:2022-01-09 21:27:37 【问题描述】:

我有一些数据,其中有 8 列。我想保留 Column、B、E、F、G、H 和 I。

在 B 列中我有一些职位,在其他列 E、F、G、H 和我有日期。我想将日期转换为 1 以计算角色,以了解日期在“角色”的“标题”中重复了多少次。

我已经手动创建了结果数据。我有大量数据,我需要多天才能达到这个结果。

如果可以使用公式实现,我将不胜感激。

数据

结果

Google Sheets Link

【问题讨论】:

您愿意接受脚本解决方案吗?如果您打算在将来对其进行修改,则可以轻松自定义。 是的,如果可能的话,我会很感激 如果可能,请查看这个@player0 【参考方案1】:

尝试:

=INDEX(QUERY(QUERY(SPLIT(FLATTEN(IF(ISDATE_STRICT(E4:I19), 
 E4:I19&"×"&B4:B19&"×"&SEQUENCE(1, 5), ), "0×a×"&SEQUENCE(5)), "×"), 
 "select Col1,Col2,count(Col2) 
  where Col2 is not null 
  group by Col1,Col2 
  pivot Col3"), 
 "offset 2", 0); "Grand Total", "", 
 TRANSPOSE(MMULT(TRANSPOSE(N(ISDATE_STRICT(E4:I19))), ROW(B4:B19)^0)))

或:

=INDEX(SORT(QUERY(QUERY(SPLIT(FLATTEN(IF(ISDATE_STRICT(E4:I19), 
 E4:I19&"×"&B4:B19&"×"&SEQUENCE(1, 5), ), "0×a×"&SEQUENCE(5)), "×"), 
 "select Col1,Col2,count(Col2) 
  where Col2 is not null 
  group by Col1,Col2 
  pivot Col3"), 
 "offset 2", 0); QUERY(QUERY(SPLIT(FLATTEN(IF(ISDATE_STRICT(E4:I19), 
 E4:I19&"×"&SEQUENCE(1, 5), ), "0×"&SEQUENCE(5)), "×"), 
 "select Col1,'Total',count(Col1) 
  where Col1 is not null 
  group by Col1 
  pivot Col2"), 
 "offset 2", 0)); "Grand Total", "", 
 TRANSPOSE(MMULT(TRANSPOSE(N(ISDATE_STRICT(E4:I19))), ROW(B4:B19)^0)))

或按您的要求:

="Date Sourced", B3, E3:I3;
 INDEX(SORT(QUERY(QUERY(SPLIT(FLATTEN(IF(ISDATE_STRICT(E4:I), 
 E4:I&"×"&B4:B&"×"&SEQUENCE(1, 5), ), "0×a×"&SEQUENCE(5)), "×"), 
 "select Col1,Col2,count(Col2) 
  where Col2 is not null group by Col1,Col2 pivot Col3"), 
 "offset 2", 0); QUERY(QUERY(SPLIT(FLATTEN(IF(ISDATE_STRICT(E4:I), 
 E4:I&"×"&SEQUENCE(1, 5), ), "0×"&SEQUENCE(5)), "×"), 
 "select Col1,'Total',count(Col1) 
  where Col1 is not null group by Col1 pivot Col2"), 
 "offset 2", 0)); "Grand Total", "", 
 TRANSPOSE(MMULT(TRANSPOSE(N(ISDATE_STRICT(E4:I))), ROW(B4:B)^0)))

demo sheet

【讨论】:

非常感谢!你是天才,令人难以置信。请再帮一个忙是否可以在公式中添加标题名称。 @HSHO 答案已更新。见演示表【参考方案2】:

如果您仍然使用应用脚本解决方案:

脚本:

function createSummary() 
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getActiveSheet();
  var timezone = spreadsheet.getSpreadsheetTimeZone();
  var locale = spreadsheet.getSpreadsheetLocale().replace('_', '-');
  var lastRow = sheet.getLastRow();
  // Assuming data starts at B3
  var startRow = 4;
  var startCol = 2;
  // Get all data (B4:I)
  var data = sheet.getRange(startRow, startCol, lastRow - startRow + 1, 8).getDisplayValues();
  // Get unique dates to iterate later
  var dates = uniqueDate(data.map(r => [r[3], r[4], r[5], r[6], r[7]]), timezone);
  // Instantiate headers
  var headers = ['Date Sourced', 'Role', 'Interested Date', 'Link', 'Screened', 'Interviewed', 'Offer'];
  // Add headers as first row in the output later
  var output = [headers];
  // Variable to store daily Totals for Grand Total
  var grandTotal = new Array(5).fill(0);

  // Iterate dates
  dates.forEach(date => 
    // Dictionary to store data
    var dateRoles = ;
    // Iterate data
    data.forEach(row => 
      // Iterate headers with date values
      headers.slice(2).forEach((header, index) => 
        // If date iterated is equal to header value for that date
        if(new Date(row[index + 3]).toDateString() == new Date(date).toDateString()) 
          // Save data in dictionary
          var role = row[0];
          var key = `$date_$role_$header`;
          // If key is existing, iterate value
          if (key in dateRoles)
            dateRoles[key]++;
          // Else, initialize to 1
          else
            dateRoles[key] = 1;
        
      );
    );
    var subOutput = [];
    // Roles present in current date with date values (sorted)
    var roles = [...new Set(Object.keys(dateRoles).map(role => role.split('_')[1]))].sort();
    // Iterate unique available roles for the date
    roles.forEach((role, i) => 
      // If first row, use date as first column, else blank
      var first = '';
      if(!i)
        first = date;

      subOutput.push([first, role, dateRoles[`$date_$role_$headers[2]`], dateRoles[`$date_$role_$headers[3]`],
                                dateRoles[`$date_$role_$headers[4]`], dateRoles[`$date_$role_$headers[5]`],
                                dateRoles[`$date_$role_$headers[6]`]]);
    );
    // Sub Totals
    var subTotals = [subTotal(subOutput, 2), subTotal(subOutput, 3), subTotal(subOutput, 4), 
                     subTotal(subOutput, 5), subTotal(subOutput, 6)];
    // Add Total per date
    subOutput.push(['', 'Total'].concat(subTotals));
    // Add sub Totals to Grand Total
    grandTotal = grandTotal.map(function (num, idx) 
      return num + parseInt(subTotals[idx] || 0);
    ); 
    // Add daily output to final output
    output = output.concat(subOutput);
  );
  // Append Grand Total
  output.push(['Grand Total', ''].concat(grandTotal));
  // Write data starting at L3
  sheet.getRange(3, 12, output.length, output[0].length).setValues(output)


// Function to get sub totals
function subTotal(data, index) 
  var total = data.map(r => r[index]).reduce((a, b) => (a || 0) + (b || 0), 0);
  if (total < 1) 
    return '';
  return total;


// Get Unique Date
function uniqueDate(data, timezone) 
  var dates = data.flat()
                  .map(date => new Date(date))
                  .filter(date => (date instanceof Date));
  return dates.map(date => date.getTime())
              .sort()
              .filter((date, i, array) => array.indexOf(date) === i)
              .map(time => Utilities.formatDate(new Date(time), timezone, "dd-MMM-yyyy"));

输出:

【讨论】:

非常感谢。学习这个脚本会很棒,很高兴你提到了 cmets。

以上是关于如何使用数据创建摘要(Google 表格)的主要内容,如果未能解决你的问题,请参考以下文章

GBQexception:如何使用存储在 Google Drive 电子表格中的大查询读取数据

关于如何使用从其电子表格的html表单接收的数据创建pdf并通过电子邮件发送pdf文件的Google应用程序脚本

如何在Google表格中为Google Data Studio数据源存储布尔值?

使用 Google Apps 脚本在 Google 表格中创建新表格

如何在 Google Apps 脚本中使用服务帐户对 Google 表格进行身份验证

如何使用 AppScript 在 BigQuery 中将 Google 工作表持久化为表格