Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Google Sheets中取消Pivot-多标题列和行_Javascript_Google Apps Script_Google Sheets_Pivot Table_Unpivot - Fatal编程技术网

Javascript 在Google Sheets中取消Pivot-多标题列和行

Javascript 在Google Sheets中取消Pivot-多标题列和行,javascript,google-apps-script,google-sheets,pivot-table,unpivot,Javascript,Google Apps Script,Google Sheets,Pivot Table,Unpivot,我有一个汇总表,大约有20列,最多有100行,但是我想将其转换为一个平面列表,以便导入数据库 在我的情况下,JS不能正常工作,我对JS的了解远远低于正确调整JS的能力 列表中有三个选项卡: 源数据-我当前拥有的虚拟数据 所需结果-我要将源数据转换为的内容 我得到的-使用上述方法时得到的结果 工作表是共享的,因此您可以尝试并测试脚本菜单>脚本>运行。它会自动创建一个新的标签。这是我在学习JS和谷歌搜索之后想到的。如果有人能建议如何使它更短/更干净/更简单-我洗耳恭听。可能还远远不够完美,但它正是我

我有一个汇总表,大约有20列,最多有100行,但是我想将其转换为一个平面列表,以便导入数据库

在我的情况下,JS不能正常工作,我对JS的了解远远低于正确调整JS的能力

列表中有三个选项卡:

源数据-我当前拥有的虚拟数据 所需结果-我要将源数据转换为的内容 我得到的-使用上述方法时得到的结果
工作表是共享的,因此您可以尝试并测试脚本菜单>脚本>运行。它会自动创建一个新的标签。

这是我在学习JS和谷歌搜索之后想到的。如果有人能建议如何使它更短/更干净/更简单-我洗耳恭听。可能还远远不够完美,但它正是我所需要的

function transpose(){
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SOURCE DATA');
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var numColumns = source.getLastColumn();
  var numRows = source.getLastRow();
  Logger.log('numColumns = ' +numColumns);
  Logger.log('numRows = ' +numRows);


  //GET NUMBER OF HEADERS (PRODUCTS)

  var products = []; // get product models in the first row
  for (var b = 2; b <= numColumns; b++){
    if (source.getRange(1, b).getValue() != "") {
      products.push([source.getRange(1, b).getValue()]); //store
    }
  }


  // PRODUCTS and SITES INTO COLUMNS

  var output = [];

  var sites = []; // get sites list
  for (var a = 3; a <= numRows; a++){
    if (source.getRange(a, 1).getValue() != "") {
      sites.push([source.getRange(a, 1).getValue()]); //store
    }
  }

  for(var p in products){
    for(var s in sites){
      var row = [];
      row.push(sites[s]);
      row.push(products[p]);
      output.push(row);//collect data in separate rows in output array
    }
   }

  var date = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "M/d/yyyy");
  Logger.log('Date = ' +date)
  ss.insertSheet(date,0).getRange(1,1,output.length,output[0].length).setValues(output);

    var newSheet = ss.getSheetByName(date);


  // COPY REGIONS

  var numProducts = products.length; // number of models
  Logger.log('numProducts = ' +numProducts);

  var i = 1;
  var j = 3 // first column number to copy
  do {
    var colC = newSheet.getRange("C1:C").getValues();
    var copyToCell = colC.filter(String).length+1;
    Logger.log('copyTo R = ' +copyToCell);

    source.getRange(3,2,numRows-2,1).copyTo(newSheet.getRange(copyToCell,3), {contentsOnly:true});
    i++;

    source.getRange(3,j,numRows-2,2).copyTo(newSheet.getRange(copyToCell,4), {contentsOnly:true});
    j+=2;  
  }
  while (i <= numProducts);
  while (j < numColumns);


  // SORT BY SITE AND PRODUCT

  newSheet.getDataRange().sort([1, 2]);
}
我认为可以使用CONCAT从location和region获取唯一的行键,并使用不需要额外脚本的方法


如果您对键连接(例如CONCATA3、~、A4)做了一些特别的操作,您甚至可以提取unpivot表中的原始位置和区域,例如REGEXEXTRACTA1、\w+~和REGEXEXTRACTA1、~\w+。

我在谷歌电子表格中编写了这个简单的通用自定义函数,用于取消激活/反向旋转。注意:这是一个自定义函数,类似于任何其他内置电子表格函数,因此您不必运行它,您可以像unique或任何其他数组函数一样使用它

在您的情况下,它与使用以下代码一样简单:=unpivotA1:H6,2,2,product,MIN,MAX

您可以在这里找到示例:

这是代码:

/**
 * Unpivot a pivot table of any size.
 *
 * @param {A1:D30} data The pivot table.
 * @param {1} fixColumns Number of columns, after which pivoted values begin. Default 1.
 * @param {1} fixRows Number of rows (1 or 2), after which pivoted values begin. Default 1.
 * @param {"city"} titlePivot The title of horizontal pivot values. Default "column".
 * @param {"distance"[,...]} titleValue The title of pivot table values. Default "value".
 * @return The unpivoted table
 * @customfunction
 */
function unpivot(data,fixColumns,fixRows,titlePivot,titleValue) {  
  var fixColumns = fixColumns || 1; // how many columns are fixed
  var fixRows = fixRows || 1; // how many rows are fixed
  var titlePivot = titlePivot || 'column';
  var titleValue = titleValue || 'value';
  var ret=[],i,j,row,uniqueCols=1;

  // we handle only 2 dimension arrays
  if (!Array.isArray(data) || data.length < fixRows || !Array.isArray(data[0]) || data[0].length < fixColumns)
    throw new Error('no data');
  // we handle max 2 fixed rows
  if (fixRows > 2)
    throw new Error('max 2 fixed rows are allowed');

  // fill empty cells in the first row with value set last in previous columns (for 2 fixed rows)
  var tmp = '';
  for (j=0;j<data[0].length;j++)
    if (data[0][j] != '') 
      tmp = data[0][j];
    else
      data[0][j] = tmp;

  // for 2 fixed rows calculate unique column number
  if (fixRows == 2)
  {
    uniqueCols = 0;
    tmp = {};
    for (j=fixColumns;j<data[1].length;j++)
      if (typeof tmp[ data[1][j] ] == 'undefined')
      {
        tmp[ data[1][j] ] = 1;
        uniqueCols++;
      }
  }

  // return first row: fix column titles + pivoted values column title + values column title(s)
  row = [];
    for (j=0;j<fixColumns;j++) row.push(fixRows == 2 ? data[0][j]||data[1][j] : data[0][j]); // for 2 fixed rows we try to find the title in row 1 and row 2
    for (j=3;j<arguments.length;j++) row.push(arguments[j]);
  ret.push(row);

  // processing rows (skipping the fixed columns, then dedicating a new row for each pivoted value)
  for (i=fixRows;i<data.length && data[i].length > 0 && data[i][0];i++)
  {
    row = [];
    for (j=0;j<fixColumns && j<data[i].length;j++)
      row.push(data[i][j]);
    for (j=fixColumns;j<data[i].length;j+=uniqueCols)
      ret.push( 
        row.concat([data[0][j]]) // the first row title value
        .concat(data[i].slice(j,j+uniqueCols)) // pivoted values
      );
  }

  return ret;
}

你好,鲁本。你说得对,我的询问不够清楚。我已经更新了。希望现在好多了