Search for values in arrays - google-apps-script

I've a spreadsheet with two sheets (Foglio1, Foglio2)
Foglio1 has five rows of numeric values in two columns, Foglio2 2 has five rows of numeric values in the first column.
I'd like to search for the Foglio2 values in Foglio1 and if it finds something copy the respective value of Foglio1, column2 into Foglio2, column2.
This is what I've got so far but it doesn't seems to work, basically it doesn't find anything so the result array remains empty
function cercaCopia() {
var ss = SpreadsheetApp.openById('myfileIDhere');
var data1 = ss.getSheetByName('Foglio2').getRange(1, 1, 5).getValues();
var data2 = ss.getSheetByName('Foglio1').getRange(1, 1, 5, 2).getValues();
var result = new Array(4);
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
if (data1[i] == data2[j, 0]) {
result[i] = data2[j, 1]
}
}
}
}

This works:
function myFunction() {
var ss = SpreadsheetApp.openById('sheetid');
var data1 = ss.getSheetByName('Foglio2').getRange(1, 1, 5).getValues();
var data2 = ss.getSheetByName('Foglio1').getRange(1, 1, 5, 2).getValues();
for (var j=0; j<5;j++){
for (var i = 0; i < 5; i++) {
if (data1[j][0]==data2[i][0]){
var range= ss.getSheetByName('Foglio2').getRange(1,1,5,5);
range.getCell(j+1,2).setValue(data2[i][1]);
}
}
}
}
I get this as a result:
In Foglio1 I had this:

Related

Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 8. Google Script

I am having troubles in
Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 8. Google Script
function myFunction() {
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Staging');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var Avals = s2.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
var resultArray = [];
for(var n=0; n < values1.length ; n++)
{
var keep = false;
var counter = 0;
for(var p=0; p < values2.length ; p++)
{
if( values1[n][1] == values2[p][1])
{
keep = true;
break ;
}
}
if(keep == false)
{
resultArray.push([values1[n]]);
//s2.appendRow( values1[n] );
s2.getRange(Alast + 1, 1, values1[n].length, values1[n].length).setValues([values1[n]]);
//s2.getRange(s2.getLastRow() + 1, 1, values1.length, values1[0].length).appendRow( values1[n] );
resultArray = [];
keep = false
}
}
}
I believe you are trying to keep appending a new row to the end of the data on "Final". You should revise you script as shown below. Use resultArray to collect all new rows and save it to spreadsheet once.
function myFunction() {
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Staging');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
// s2.getDataRange() will get up to the last row of data
var Alast = values2.length;
var resultArray = [];
for(var n=0; n < values1.length ; n++)
{
var keep = false;
var counter = 0;
for(var p=0; p < values2.length ; p++)
{
if( values1[n][1] == values2[p][1])
{
keep = true;
break ;
}
}
if(keep == false)
{
resultArray.push(values1[n]);
keep = false
}
}
if( resultArray.length > 0 ) {
s2.getRange(Alast+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
}

How do I add the values from three arrays?

Is there an easy way to add the elements of arrays together? Let's say I have 3 arrays.
[15,22,35,40]
[10,20,12,20]
[11,24,14,22]
What I want to do is add up the totals so it would be:
[36,66,61,82]
and then overwrite [11,24,14,22] with those values.
Forgot to add the code I already have:
function combineItems(){
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == "Product A") {
medData = sheet.getRange(i + 1, 7, 1, 4).getValues();
console.log(medData);
}
if (values[i][j] == "Product B") {
smData = sheet.getRange(i + 1, 7, 1, 4).getValues();
console.log(smData);
}
if (values[i][j] == "Product C") {
newData = sheet.getRange(i + 1, 7, 1, 4).getValues();
console.log(newData);
}
}
}
}
So I want to add up Product A, Product B, and Product C and then overwrite Product C with those new totals.
Solution:
function myFunction(data) {
return data[data.length-1].map((i, index) => i =
data.reduce((prev, curr) => prev + curr[index], 0)
)
}
Example:
const productA = [15,22,35,40]
const productB = [10,20,12,20]
const productC = [11,24,14,22]
const newData = myFunction([productA, productB, productC])
/**
Expected Output:
[36.0, 66.0, 61.0, 82.0]
**/
How It Works:
Taking the input of a multidimensional array, we alter the last row in place (.map()) by reducing (.reduce()) all values of the input data by index for each row.
(Reduce each column of the data into the last row.)
Suggested Reading:
Array.map()
Array.reduce()

Is there a i.height parameter in appscript?

i tried to find any info on it, i want to use height of the row in a cycle, i know that there is i.length, but is it one for height?
example with length:
for (var i = 2; i <= data.length+1; ++i)
i want it to look something like:
for (var g = 1; g<=grupa.height+1;g++)
this is my whole code, if you want for some reason to look at it:
function myFunction()
{
const ss = SpreadsheetApp.openByUrl ('my url');
var sheet = ss.getSheetByName("Розклад")
var data = sheet.getRange(2, 1, sheet.getLastRow()-1 , sheet.getLastColumn()).getValues();
Logger.log(data);
for (var i = 2; i <= data.length+1; ++i)
{
var data1 = sheet.getRange(i,1,1,13).getValues()
var data2 = sheet.getRange(i,8,1,13).getValues()
var grupas = data [i-2][4]
var cell= data [i-2][8]
var grupa = ss.getSheetByName(grupas)
var grupav = grupa.getRange(1,1,grupa.getLastRow(), grupa.getLastColumn()).getValues();
for (var g = 1; g<=grupa.height+1;g++)
{
var grupap = grupa[0][g-1]
Logger.log(grupap)
}
Logger.log(grupas)
Logger.log(grupav)
Logger.log(cell)
Logger.log(data1);
if ( cell ==="")
{
Logger.log('blank cell');
}
else
{
Logger.log(data2)
}
Logger.log(i-1);
}
}
var grupa = ss.getSheetByName(grupas)
var grupav = grupa.getRange(1,1,grupa.getLastRow(), grupa.getLastColumn()).getValues();
for (var j = 0; j < grupav.length; j++) {
/* grupav[j][0] */
}
Row loop
const col = 0;
const values = sheet.getDataRange().getValues();
for (const i = 0; i < values.length; i++) {
/* values[i][col] */
}
Column loop
const row = 0;
const values = sheet.getDataRange().getValues();
for (const j = 0; j < values[0].length; j++) {
/* values[row][j] */
}
Row & Column loop
const values = sheet.getDataRange().getValues();
for (const i = 0; i < values.length; i++) {
for (const j = 0; j < values[0].length; j++) {
/* values[i][j] */
}
}

writing a count program to find the number of repeatitions

I am writing a code to count the number of times a time duration in column B is repeated.After writing a code according to my logic I am still getting error or no output. Here's the app script code I have written. Help me out.
function count() {
var data = SpreadsheetApp.getActive();
var sheet = data.getSheetByName("Sheet4");
var lastRow = sheet.getLastRow();
var sh = sheet.getRange('B1:B' + lastRow);
var cell = sh.getValues();
var count = 0;
for (var i = 0; i < lastRow; j++) {
var column2 = cell[i][1];
for (var j = 0; j < i; j++) {
var column4 = cell[j][1];
if (column4 == column2) {
count++;
}
}
sheet.getRange('F' + (i + 1).toString()).setValue(count);
}
}
This is the intended output instead of column F i put the sample output in column C
Issue(s):
Outer loop's increment variable doesn't change.
for (var i = 0; i < lastRow;/*i never changes =>*/ j++)
Date/Time are retrieved as JavaScript objects and cannot be compared with ==:
if (column4 == column2) {//Date objects cannot be compared
cell doesn't have a value at array index [1]
Solution:
Increment all variables in the for -loop
Compare value of date objects instead
Array indexes start at [0]
Snippet:
for (var i = 0; i < lastRow; ++i) {
var column2 = cell[i][0];
/*....*/
var column4 = cell[j][0];
if (column4 - column2 === 0) {//#see https://stackoverflow.com/questions/492994

Google Sheet script getValues and when find the given value, replace the value of the above row

How can I modify the script below to read the values in a column, but only read the values of the specified cells: L9, L10 ... L18, L19 .... L27, L28 .... (two by two jumping eight, to the end of the column L2000) and when find the value "1" in one of those cells, then replace the value that is 4 lines up with "FALSE"?
e.g. if cell L19 is found the number 1, then replace the value of cell L15 with the word "FALSE" ... and if cell L28 is found the number 1, then replace the value of cell L24 with the word "FALSE"
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ABC");
var lastrow = ss.getLastRow();
var range = ss.getRange(9, 12, lastrow - 1, 1);
var data = range.getValues();
for (var i=0; i < data.length; i++) {
if (data[i][0] == "1") {
data[i][0] = "FALSE";
}
}
range.setValues(data);
Add another counter to loop: Try Modifying
From:
var range = ss.getRange(9, 12, lastrow - 1, 1);
var data = range.getValues();
for (var i=0; i < data.length; i++) {
if (data[i][0] == "1") {
data[i][0] = "FALSE";
}
}
To:
var range = ss.getRange(5, 12, lastrow+5, 1);
var data = range.getValues();
var m= 0;
for (var i=4; i < data.length; ) {
if (data[i][0] == "1") {
data[i-4][0] = "FALSE";
}
m++;
if (m == 2) {
m=0;
i = i + 8;
} else {
i++;
}
}
My take on what the loop should become:
var range = ss.getRange(9, 12, lastrow - 1, 1);
var data = range.getValues();
var i = 4,
// Subtract 1 here because we compare both i and i+1 in the same loop body
len = data.length - 1;
while (i < len) {
// Check the value of the first row in the subset.
if (data[i][0] == 1) {
data[i - 4][0] = "FALSE";
}
// Check the next row in the subset.
if (data[++i][0] == 1) {
data[i - 4][0] = "FALSE";
}
// Advance to the next subset.
i += 8;
}