【问题标题】:Google App Scripts(spreadsheet) - consolidate data into one sheetGoogle App Scripts(电子表格) - 将数据合并到一张表中
【发布时间】:2014-04-27 01:05:38
【问题描述】:

这里是设置

我们根据项目分数与所有员工进行竞赛。每个项目有两类员工(每个类别 4 名员工)和两个分数(每个类别的员工一个)。

我需要获取员工的所有分数并将其输出到电子表格中。 The following spreadsheet has misc. columns removed

表格说明
标有“示例数据”的工作表是我们将从中提取数据的来源

  1. 我们需要匹配编辑器和编辑器分数
  2. 我们需要匹配站长和站长分数

标记为“示例输出”的工作表是我希望在另一个名为“竞赛结果”的电子表格中生成的,其工作表名称来自源工作表(它们按日期范围命名)。

  1. 我们需要按类别编译每个员工
  2. 我们需要将单个员工的所有分数汇总到行中

我发现这个Removing Duplicates Article 似乎至少可以处理信息并以我认为可以完成的方式进行比较,但由于缺乏经验而未能使其工作。

直到有人评论才知道 Transpose 是什么 :) 这是另一篇文章中关于如何使用 Google Apps 脚本和使用电子表格选项来实现它的解决方案。 How to split and transpose results over 2 columns

这是我用来让它工作的实际代码(有点可怕,但我试过了)关于如何改进它的建议?:

function createScoreSheet() {

  // Get Source spreadsheet
  var source = SpreadsheetApp.getActive();
  var sourceSheet = source.getActiveSheet();
  var SourceActivate = sourceSheet.activate();
  // Set Sheet Name
  var sheetName = sourceSheet.getSheetName();
  // Set Values to transpose and combine
  var sourceEditor = sourceSheet.getRange("C1:C51");
  var sourceWeb = sourceSheet.getRange("D1:D51");
  var editorScores = sourceSheet.getRange("L1:L51");
  var webScores = sourceSheet.getRange("K1:K51");
  // Used to create a new spreadsheet
  var sheetNameNew = sheetName + " Scores";
  var createSheet = SpreadsheetApp.getActive().insertSheet(sheetNameNew,0);
  var targetSheet = source.getSheetByName(sheetNameNew);
  var totalScore = 1;
  // s is the the counter we use to stick values into the rows
  var s = 3;
  // n is the the counter we use to stick values into the columns
  var n = 1;
  // loops through twice, once for the editor values, once for the webmaster
  for (var j = 1; j<3; j++) {
    if (j == 1) {
      // grab values for the editors and copy to new sheet
      sourceEditor.copyTo(targetSheet.getRange("A1"));
      editorScores.copyTo(targetSheet.getRange("B1"));
      // delete the header row then sort the column ASC by default     
      targetSheet.deleteRow(n);
      targetSheet.sort(1);
      // Find the last value to see how many scores we have
      var lastRow = targetSheet.getLastRow();
    }
    if (j == 2) {
      // grab values for the webmasters and copy to new sheet
      sourceWeb.copyTo(targetSheet.getRange(n,1));
      webScores.copyTo(targetSheet.getRange(n,2));
      // delete the header row then sort the column ASC by default 
      targetSheet.deleteRow(n);
      lastRow = targetSheet.getLastRow();
      targetSheet.getRange(n,1,lastRow,2).sort(1);
      lastRow = targetSheet.getLastRow();
    }
    // this loop will check to see if the value of the cell is equal to the next on the list and move the score
    for (var i = 1; i<lastRow+1; i++) {
      // Grab the name of the current row and the next
      var firstName = targetSheet.getRange(n,1).getValue();
      var nextName = targetSheet.getRange(n+1,1).getValue();
      // Grab the scores
      var oldScore = targetSheet.getRange(n+1,2);
      var newScore = targetSheet.getRange(n,s);
      // Loop to check to see if the firstname is blank and break to find the next value
      if (firstName === "") {
       break; 
      }
      // checks to see if name is equal to the next then shifts then copies the score and adjust the horizontal position
      if (firstName == nextName) {
        totalScore = oldScore + newScore;
        oldScore.copyTo(newScore);
        s = s+1;
        targetSheet.deleteRow(n+1);
      }
      // resets horizontal position for the score and increases the row 
      else {
        s=3;
        n=n+1;
      }
    }
    // kills remaining rows
    targetSheet.deleteRows(n,37);
  }
}

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    我会这样做:

    如果您也想自动生成名称,请将其写入输出表 A1:

    =unique('Example Data'!B2:B) - 此函数只是为 A2-A5 单元格生成编辑器名称。

    现在将这个写入 B2 单元格:

    =transpose(filter('Example Data'!E:E,'Example Data'!B:B=A2)) - 此函数根据行首的给定名称过滤编辑器点(在此案例其 A2)。然后将结果转置为水平形式。要获取其他行的结果,只需向下填充此公式即可。

    我想你可以找出其余的。 :)

    希望对你有帮助。

    【讨论】:

    • 这确实有帮助,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2012-08-12
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多