¿Cómo puedo transponse una columna de datos en filas de datos, utilizando celdas vacías como indicadores para comenzar una nueva fila?

votos
1

Para empezar, aquí está mi hoja que da dos ejemplos de conjuntos de datos y los resultados deseados para cada uno.

https://docs.google.com/spreadsheets/d/1MPppt2yztfPtz2iSssSfIuBoYccxQ4gs7PZdygdz1Z8/edit?usp=sharing

Aquí está el código que tengo en este momento. No está en un estado de trabajo, pero parece estar cerca. Editará como yo experimento diferentes soluciones.

// on open, add menu and bttons to test functions
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Analyze and flip data selection', functionName: 'flipSelection'},
    {name: 'Check for first empty cell', functionName: 'firstEmptyCell'}
  ];
  spreadsheet.addMenu('Scripts', menuItems);
}


// put data from a column into a row. on a blank line, write to new row
function flipSelection() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var testSheet = ss.getSheetByName(test);
  var sheet = ss.getSheetByName(main);
  var range = sheet.getActiveRange();
  var numRows = range.getNumRows();
  var numCols = range.getNumColumns();
  var data = [];

  // check active range(current selection) for values and store them in data array
  for (var i = 1; i < numRows; i++){
    for (var j = 1; j < numCols; j++) {
      var currentValue = range.getCell(i,j).getValue();
      data.push(currentValue);
    }
  }

  // log array to verify accurate data
  for (var k = 0; k < data.length; k++){
    ss.toast(data[k].toString());
    Logger.log(data[k].toString());
  }

  // for each string in data array, write to next cell. if the string is blank, go to next row and write to next cell (recursion??)
  for (var n = 0; n < data.length; n++) {
    // Do we have data? If not, move on
    if (data[n] == ) {
      //target next empty row (need help here!)
      continue;
    }

    const colA = testSheet.getRange('A:A').getValues().join().split(,);
    const rowIndex = colA.indexOf('');
    const colIndex = firstEmptyCell(rowIndex); // assuming firstEmptyCell works correctly
    const targetCell = testSheet.getRange(rowIndex, colIndex);

    targetCell.setValue(data[n]);
  }




}


// a way to find first empty cell 
function firstEmptyCell (emptyRow) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var testSheet = ss.getSheetByName(test);
  var range = testSheet.getRange(emptyRow, 1, 1, testSheet.getMaxColumns());
  var vals = range.getValues();

  // Get the index of the first empty cell from the waarden array of values
  var emptyCell = vals[0].indexOf();

  // log the index of empty cell
  ss.toast(The index of the first empty cell is:  + emptyCell.toString());
  Logger.log(The index of the first empty cell is: %s, emptyCell);
  return emptyCell;
}
Publicado el 19/09/2018 a las 17:13
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
1

probar este código:

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Analyze and flip data selection', functionName: 'flipSelection'},
  ];
  spreadsheet.addMenu('Scripts', menuItems);
}

function flipSelection()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet= ss.getSheetByName("src");
  var destSheet= ss.getSheetByName("dest");
  //get datas
  var datas = sheet.getDataRange().getValues();
  var destDatas = [];
  var row = [];
  var max = 0;
  var i = 0;
  //start is to check if it's the beginning of parse meaning no previous data
  var start = true;
  //parse columns first then rows
  for (var c = 0; c < datas[0].length; c++)
  {
    for (var r = 0; r < datas.length; r++)
    {
      //if cell is blank or beginning of new column
      if (datas[r][c] == "" || !r)
      {
        i = 1;
        //if it's not the beginning of program then add previous row to final data array.
        if (!start)
        {
          destDatas.push(row);
        }
        //if it's a blank cell then add skip it
        if (r)
          r++;
        //create a new row with first value inside of it
        row = [datas[r][c]];
        start = false;
      }
      //if it's not the beginning of a new column and the cell is not empty
      else
      {
        //increment index columns of one
        i++;
        // Basically if there's more value than last time then set column length to column index
        if (i >= max)
          max = i;
        //add new data to row array
        row.push(datas[r][c])
      }
    }
  }
  //condition to push previous and last row into final data array
  if(row)
    destDatas.push(row);
  //set final data array length to the same size 
  for(var i = 0; i < destDatas.length; i ++)
    for (var j = (destDatas[i].length); j < max; j++)
      destDatas[i].push("");
  //set destination range and set values.
  destSheet.getRange(1, 1, destDatas.length, max).setValues(destDatas);
}
Respondida el 19/09/2018 a las 19:58
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more