Domanda

So with Google Apps Script, I have a spreadsheet as following:

A       B       C...
apple   banana  orange...
banana  orange  grape...
banana  apple   orange...
...     ...     ...

...and I want to make it like this:

apple   2
banana  3
grape   1
orange  3

What would be the easiest way to make this possible?

I have tried to use query function on Google Apps Spreadsheet, but since there are multiple columns it is difficult to sort and count. I have also tried to use join and countif function but it tend to get redundant since there are too many columns.

Need help!

È stato utile?

Soluzione

this should do the work:

function myFunction() {
  var objList={};
  var ss = SpreadsheetApp.getActive().getActiveSheet();
  var data = ss.getDataRange().getValues();
  for(var i in data){
    for(var j in data[i]){
      if(typeof objList[data[i][j]]=="undefined"){
        objList[data[i][j]]=1;
      }
      else{
        objList[data[i][j]]+=1;
      }
    }
  }
  var objTable=[];
  for(var k in objList){
   objTable.push([k,objList[k]]);
  }
  Logger.log(objTable);
  ss.clear();
  ss.getRange(1, 1, objTable.length, objTable[0].length).setValues(objTable);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top