function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('data');
var data = sheet.getDataRange().getValues();
var range = sheet.getRange("A1:L" + data.length);
range.sort(1);
const people = {};
for(var i = 0; i < data.length; i++) {
var name = data[i][0] + data[i][1];
console.log(i);
if (!people.name) {people.name = {rows: [i]};} else {people.name.rows.push(i)}
}
Logger.log(people);
}
What should I be doing differently? At the end, it logs {name={rows=[0.0, 1.0, 2.0, ...]}} instead of having an object for each name...?
In the sheet there's just a first name and last name on columns A and B, for around 80 rows.
Use the bracket syntax if you want to use dynamic names for properties: https://riptutorial.com/javascript/example/2321/dynamic---variable-property-names
In your case:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('data');
var data = sheet.getDataRange().getValues();
var range = sheet.getRange("A1:L" + data.length);
range.sort(1);
const people = {};
for(var i = 0; i < data.length; i++) {
var name = data[i][0] + data[i][1];
console.log(i);
if (!people[name]) {people[name] = {rows: [i]};} else {people[name].rows.push(i)}
}
Logger.log(people);
}
I want to get sums of numeric values for each separate column in the range. What I do wrong?
function countNutrients(){
var sh = SpreadsheetApp.getActiveSheet();
var lastcol = sh.getLastColumn(); //Get last column
var lastcolval = sh.getRange(1, 1, 1, lastcol).getValues();
var lastrowval = sh.getRange("C1:C").getValues(); //Trick to get last row
var lastrow = lastrowval.filter(String).length; //
var sumvalues = sh.getRange(2, 14, lastrow, lastcol).getValues();
(typeof sumvalues === 'number' ? "" : sumvalues);
Logger.log(sumvalues);
for (var j = 13; j < lastcolval.length ; j++) {
var sumcolumnval = sh.getRange(2, [j], lastrow);
var sum = 0;
for (var i in sumcolumnval[0]) {
var sum = sumvalues[0][i] + sum;
}
return sum
Logger.log(sum);
}
return sumcolumnval
}
Columns on the screenshot and 65 more
enter link description here
CSV example
Sum of Columns With a Script
function sumOfCols() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet14');
var hA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
var sum={};
hA.forEach(function(h,i){sum[h]=0;});
var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
var v=rg.getValues();
v.forEach(function(r,i){r.forEach(function(c,j){sum[hA[j]]+=c;});});
var html='';
hA.forEach(function(h,i){html+=Utilities.formatString('<br />%s(sum): %s',h,sum[h]);});
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(ui, "Column Sums")
}
Spreadsheet:
Output Dialog:
Data(csv):
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10
1,2,3,4,5,6,7,8,9,10
2,3,4,5,6,7,8,9,10,11
3,4,5,6,7,8,9,10,11,12
4,5,6,7,8,9,10,11,12,13
5,6,7,8,9,10,11,12,13,14
6,7,8,9,10,11,12,13,14,15
7,8,9,10,11,12,13,14,15,16
A function for your particular Application
This will sum just the six columns that you selected
function countNutrients(){
var sh=SpreadsheetApp.getActiveSheet();
var hA=sh.getRange(1,14,1,6).getValues()[0];
var sum={};
hA.forEach(function(h,i){sum[h]=0;})
var vA=sh.getRange(2,14,sh.getLastRow()-1,6).getValues();
vA.forEach(function(r,i){
r.forEach(function(c,j){
if(!isNaN(c)) {
sum[hA[j]]+=c;
}
});
});
var html="";
var keys=Object.keys(sum);
keys.forEach(function(k,i){
html+=Utilities.formatString('<br />sum[%s]=%s',k,sum[k]);
})
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(ui,'Sums of Cols N through S')
}
I'm currently developing a sheet that shows results from a set of data based on some filters but the data loads to slowly when getting the results, I've tried to follow the Best Practices from Google Documentacion with no luck, how can I set an array for the data to load faster?
Below is the code commented with what I've already tried
function realizarBusqueda() {
var inicio = SpreadsheetApp.getActive().getSheetByName("INICIO");
var aux_tags = SpreadsheetApp.getActive().getSheetByName("Aux_Tags");
var data = SpreadsheetApp.getActive().getSheetByName("Data");
var data_lc = data.getLastColumn();
var data_lr = data.getLastRow();
var searchRange = data.getRange(2,1, data_lr, data_lc);
var inicio_lc = inicio.getLastColumn();
inicio.getRange("A8:L1000").clearContent();
inicio.getRange("A8:L1000").clearFormat();
var countRows = inicio.getMaxRows();
inicio.deleteRows(20, (20-countRows)*-1);
if (inicio.getRange("B4").isBlank()) {
inicio.getRange("A8:L1000").clearContent();
inicio.getRange("A8:L1000").clearFormat();
var countRows = inicio.getMaxRows();
inicio.deleteRows(20, (20-countRows)*-1);
SpreadsheetApp.flush();
}
else if ((inicio.getRange("B4").getValue() != "" &&
inicio.getRange("C4").getValue() === "")) {
//filtrado 1
var arrayDatos = searchRange.getValues();
var inicio_fr = 8;
//var row = new Array(11);
for (var j = 2; j <= data_lr; j++) {
//row[j] = new Array(data_lr);
if (aux_tags.getRange("P2").getValue() === arrayDatos[j-2][4]) {
var inicio_fc = 1;
for (var i = 0; i < arrayDatos[j-2].length; i++) {
//row[j][i] = arrayDatos[j-2][i];
var row = arrayDatos[j-2][i];
inicio.getRange(inicio_fr, inicio_fc).setValue(row);
inicio_fc++;
}
inicio_fr++;
}
//inicio.getRange("A8").setValues(row);
}
}
I expect the output to load lots faster, currently what I've tried is commented, the code as-is is working but too slow
I just wanted to update this subject because I figured out myself, see attached the new code with the use of new 2D arrays
...
//filtrado 1
var arrayDatos = searchRange.getValues();
var inicio_fr = 8;
var rows = [];
var row = [];
for (var j = 2; j <= data_lr; j++) {
if (aux_tags.getRange("P2").getValue() === arrayDatos[j-2][4]) {
var inicio_fc = 1;
for (var i = 0; i < arrayDatos[j-2].length; i++) {
row.push(arrayDatos[j-2][i]);
if (i == 11) {
rows.push(row);
row = [];
}
}
}
}
inicio.getRange(8, 1, rows.length, rows[0].length).setValues(rows);
}
Now instead of writing on row at a time, I just write the whole array at once
I want to do a comparate a elementes inside of an array for get a specific value, how i could do it with this?
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
dataToImport = {
Nivel:data[i][0],
Consumo:data[i][1],
Flujo:data[i][2],
if(dataToImport[Nivel]>99)
{
}
I'm not sure what your looking for but hopefully this will help.
function myfunction()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var data=sh.getDataRange().getValues();
var dataToImport = {};
for(var i=1;i<data.length;i++)
{
dataToImport = {'Nivel':data[i][0],'Consumo':data[i][1],'Flujo':data[i][2]}
if(dataToImport['Nivel']>99)
{
Logger.log('Nivel greater than 99 dataToImport = %s', dataToImport);
}
Logger.log(dataToImport);
}
}
I need help to get a list of unique sorted value of a specific column (column B).
This is my try (It doesn't work), can you help me to find the error?
I'm not able to get the correct range in [object] format...
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function UniqueRange() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1);
var rngA = rng.getValues();
var shtout = s.getSheetByName('Nuovo');
var rngout = shtout.getRange(2,1,rngA.length,1)
var rngB = rngout.setValues(rngA);
var i = 0; i < rngA.length; i++
var rngB = rngB[i];
var unique = rngB.filter( onlyUnique );
}
Solution by Bellian:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function uniquevalues(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1); //range that contain all values
var rngA = rng.getValues();
var unique = [];
for(var i = 0; i < rngA.length; i++) {
var unique = unique.concat(rngA[i+1]);
}
unique = unique.filter(onlyUnique);
Logger.log(unique)
}
function getUniqueSortedArray(sheetname,column,datastartrow)
{
if(sheetname && column && datastartrow)
{
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName(sheetname);
var cntRg=cntSh.getDataRange().sort({column:column,ascending:true});
var vA=cntRg.getValues();
var uiA=[];
for(var i=datastartrow-1;i<vA.length;i++)
{
if(uiA.indexOf(vA[i][column-1])==-1)
{
uiA.push(vA[i][column-1]);
}
}
return uiA;
}
else
{
return 'Invalid Inputs';
}
}
And this is my modification of your code.
function UniqueSortedRange()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName('Prova')
var rg=sh.getDataRange().sort({column:2,ascending:true});
var vA=rg.getValues();
var sh0=ss.getSheetByName('Nuovo');
var luneeko=[];
for(var i=1;i<vA.length;i++)
{
if(luneeko.indexOf(vA[i][1])==-1)
{
luneeko.push(vA[i][1]);
}
}
return luneeko;
}