首页 > 解决方案 > 转置/旋转、匹配和索引?不知道如何处理这种情况

问题描述

我已经搞砸了很长一段时间,但我无法弄清楚如何让数据“旋转”并匹配我想要的方式。我已经尝试了大约 100 种不同的场景,但谷歌床单不是我的强项。

基本上我有一堆原始输入数据,所有输入数据都有静态点值(但每个输入值都有1000个潜在的“项目”)可能有超过一百个“人”有一个静态列表项目和相关值。

期望的结果基本上是所有项目的列表(在第二个电子表格中已经硬编码并按类别分组)。所有潜在的“人”和他们在第一个电子表格中从最高到最低分配项目的值。

原始数据

期望的结果

下面的示例: https ://docs.google.com/spreadsheets/d/1CyynGtXublw_0nOF7aulXaX8bA5KmPlGJ76mRcJvIog/edit?usp=sharing

标签: google-sheets

解决方案


解决方案

这是我使用Apps Script功能找到的解决方案。如果您不确定如何使用它,请询问我。要访问脚本编辑器,请在表格菜单栏上转到工具 -> 脚本编辑器并粘贴以下代码。在您需要Solution使用您在示例工作表中提供的硬编码信息(即项目编号列)创建工作表之前。其余的将相应地动态添加。

以下代码具有不言自明的注释:

function myFunction() {
  // Get origin Sheet where we have our unordered data
  var originSheet = SpreadsheetApp.getActive().getSheetByName('Raw Data');
  // Get sheet where we will order the data
  var destinationSheet = SpreadsheetApp.getActive().getSheetByName('Solution');
  
  // Get the range on the unordered data sheet where the items will be placed
  var values = originSheet.getRange('B2:E6').getValues();
  // Get the values (names) of the items in the ordered data sheet (for instance #item1)
  var items = destinationSheet.getRange('A3:A8').getValues().flat();
  
  // Iterate over all the columns first and then rows of the range of unordered data
  // where we have all of our items. 
  for(i=0;i<values.length;i++){ // columns
    for(j=0;j<values[i].length;j++){ // rows
      
  // Iterate over all the possible items in our ordered data, from item1 to item5
      for(k=0;k<items.length;k++){
        
/* If the value of the unordered data cell we are checking matches the value
 of the item name then we know that the information of that unordered value
 belongs to that item. For example, in the first case if the first cell in row 2 and col 2
of the underered data matches item1 (which does) then we know we must provide the information
to that ordered item value */
        
        if(values[i][j]==items[k]){
          
          // This is for checking the last column with a value to avoid writing over blank cells
          var lastCol=0;
          // Iterate over 10 columns
          for(y=0;y<10;y++){
          // If the column in the row of item1 is empty, then we must write the information there
            if(destinationSheet.getRange(k+3,y+2).getValue()==""){
              lastCol=y+2;
              break;
            }
          }
          // Now that we know where we must write the info, with the information of the row and column values
          // of the cell we can get their corresponding elements and make a string where we get them together to 
          // finally set the right cell in the destination sheet
          destinationSheet.getRange(k+3,lastCol).setValue(originSheet.getRange(i+2,1).getValue()+ ' - '+originSheet.getRange(1, j+2).getValue());
          
        }
      }
    }  
  }
}

我希望这对你有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。:)


推荐阅读