Compare multiple values in one array - javascript

I need some help determining how i can compare 4 columns of one row with 4 columns of another row.
To explain, I have two tables, which both have four columns: State, Area, City & Location.
PINPOINT table - this table has a unique combination of values
FEED table - this table has a recurring combination of values, as well as the KEY value from the PINPOINT Table.
What i want is a code that says
"If a row from column CL in the FEED sheet contains the Key Value from column K in the PINPOINT sheet,
...but columns CN to CQ that FEED sheet row doesn't have the same values as column A-D in the PINPOINT table...
update column CN to CQ the FEED sheet with the same combination of values as A-D in the PINPOINT table."
I pasted the latest code i have below, as well as the images. Admittedly, this code is a mess I just started learning code, so I'm happy to rewrite this if someone proposes a solution.
In any case, any insight into how i should write this will be quite helpful.
var Data = SpreadsheetApp.getActiveSpreadsheet(); // DATA spreadsheet
var PinpointDataSheet = Data.getSheetByName("The Pinpoints") // DATA "Pinpoint" sheet
var PinpointAllValues = PinpointDataSheet.getRange(2, 1, PinpointDataSheet.getLastRow()-1,PinpointDataSheet.getLastColumn()).getValues();
var FeedDataSheet = Data.getSheetByName("The Feed_Raw") // DATA "Feed" sheet
var FeedAllValues = FeedDataSheet.getRange(2, 1, FeedDataSheet.getLastRow()-1,FeedDataSheet.getLastColumn()).getValues();
var PinpointStateObj = {}; // Object for "Locale" values
var PinpointAreaObj = {}; // Object for "Locale" values
var PinpointCityObj = {}; // Object for "Locale" values
var PinpointSpotObj = {}; // Object for "Locale" values
for(var P = PinpointAllValues.length-1;P>=0;P--) // put Pinpoint values in array..
{
PinpointStateObj[PinpointAllValues[P][0]] = PinpointAllValues[P][10];
PinpointAreaObj[PinpointAllValues[P][1]] = PinpointAllValues[P][10];
PinpointCityObj[PinpointAllValues[P][2]] = PinpointAllValues[P][10];
PinpointSpotObj[PinpointAllValues[P][3]] = PinpointAllValues[P][10];
}
for(var F = FeedAllValues.length-1;F>=0;F--) // for each row in the "Feed" sheet...
{
var Feed_GeotagKey = FeedAllValues[F][90]; // Pinpoint Key values in Feed sheet
{
// If Pinpoint array dont match feed values
if ((PinpointStateObj[Feed_GeotagKey] != FeedAllValues[F][95]) || (PinpointAreaObj[Feed_GeotagKey] != FeedAllValues[F][96])
|| (PinpointCityObj[Feed_GeotagKey] != FeedAllValues[F][97]) || (PinpointSpotObj[Feed_GeotagKey] != FeedAllValues[F][97]))
{
FeedAllValues[F][95] = PinpointAllValues[P][0]; // ...Change FYI Category Name in FYI Topic Sheet
FeedAllValues[F][96] = PinpointAllValues[P][1];
FeedAllValues[F][97] = PinpointAllValues[P][2];
FeedAllValues[F][98] = PinpointAllValues[P][3];
}
}
}
Geotag Sheet - unique values "Dark column"
Feed Sheet - recurring values - "Highlighted column"

Related

Retrieving all values in a column

I am new to google apps script and I was trying to get all the values in a particular column inside a sheet named "Items". I was able to create a loop to get to the last row that contains value but when I try to use the function, no data is retrieved. I tried console.log(values[lr][0]); inside the if clause and it outputs just fine.
Here's my code
function getAllItems()
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var locationSheet = ss.getSheetByName("Items");
var values = locationSheet.getRange("Items!B2:B").getValues();
for(var i = values.length - 1 ; i >= 0 ; i--){
if (values[i][0] != null && values[i][0] != ""){
lr = i + 1;
values.sort();
return values[lr][0];
}
}
}
There are several ways to retrieve values from a column in Google Sheets.
The basics, getting the sheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Items');
SpreadsheetApp.getActiveSpreadsheet() works in bounded projects and add-ons. Spreadsheet.getSheetByName(name) works when the sheet name is known.
Getting the column values by using Sheet.getRange and an open reference using A1 notation
var values = sheet.getRange('B:B').getValues();
If your spreadsheet has blank rows at the bottom, in this case Range.getValues besides the column values, it will return an empty string for each blank rows.
Besides using Sheet.getRange with an open reference, it might be used with other reference types and using start row, start column, number of rows and number of columns.
Getting the column values by using Sheet.getRange and an open reference using A1 notation excluding empty strings
var values = sheet.getRange('B:B').getValues().filter(String);
Getting the column values by using Sheet.getDataRange and Array.prototype.map
var values = sheet.getDataRange().getValues().map(row => row[1]);
Only will return the values from the first row to the last row of the data range. The data range is determined from A1 to the last row and last column having values, i.e., if one column B have values from row 1 to row 10 and column C have values from row 4 to row 20, the data range reference is A1:C20, so values will contain the values from row 1 to row 20, showing empty strings for the blank cells.
Getting the column values by using Sheet.getDataRange, Array.prototype.splice and Array.prototype.getLastIndex
var values = sheet.getDataRange().getValues();
values.splice(values.findLastIndex(String) + 1);
Only will return the values from the first row to the last row of the column containing non empty strings. This might be helpful when having columns "of different sizes", as explained in the previous case. Please note that if there blank cells in between, an empty string will be included as value of these cells.
Notes:
Instead of Range.getValues you might use Range.getDisplayValues to get the strings with the values formatted as strings as they are displayed on Google Sheets cells. Both methods return the values structured as an Array of Arrays, this might be handy if you will be adding the values to another range, but if you want to add them to the execution logs you might want to format them in another way.
Please bear in mind that if the column content is very large, nowadays a Google Sheets spreadsheet could have up to 10 million cells and each cell could have upto 50k characters, the column content will be truncated when printed to the execution logs.
Related
Get column from a two dimensional array
Resources
Array
You don't need a loop for that (explanation in comments):
function getAllItems()
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var locationSheet = ss.getSheetByName("Items");
var values = locationSheet.getRange("Items!B2:B").getValues().flat(); // 2D -> 1D array
var filter_values = values.filter(r=>r!=''); // remove empty rows
Logger.log(filter_values); // get the full list
Logger.log(filter_values[filter_values.length-1]); // get the last value;
return filter_values[filter_values.length-1];
}
Try this:
function getAllItems(){
var ss= SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Items");
var vs = sh.getRange("B2:B"+sh.getLastRow()).getValues();//all the values in column B
return sh.getLastRow();//the last row with data
}
Or you can use:
function getColumnHeight(col, sh, ss) {
var ss = ss || SpreadsheetApp.getActive();
var sh = sh || ss.getActiveSheet();
var col = col || sh.getActiveCell().getColumn();
var rcA = [];
if (sh.getLastRow()){ rcA = sh.getRange(1, col, sh.getLastRow(), 1).getValues().flat().reverse(); }
let s = 0;
for (let i = 0; i < rcA.length; i++) {
if (rcA[i].toString().length == 0) {
s++;
} else {
break;
}
}
return rcA.length - s;
//const h = Utilities.formatString('col: %s len: %s', col, rcA.length - s);
//Logger.log(h);
//SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(h).setWidth(150).setHeight(100), 'Col Length')
}
function getAllItems(){
var ss= SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Items");
var vs = sh.getRange("B2:B"+getColumnHeight(2,sh,ss).getValues();//all the values in column B
return sh.getLastRow();//the last row with data
}
If you use filter() to filter out all of the nulls you may not get the desired result if one of the data elements is null.

how to change value in a range of cell to a data array in google AppScript?

How to pull data in a range of cell and then push those values into an array of data?
In this code I tried to get those values in range E2:E97 and then push those values to an array such as [E2 value, E3 Value, etc] then set value to database sheet using dataS.getRange(dataS.getLastRow()+1).setValue(value);
but it seems I can't get it done with those code. so any idea to do this?
// Save Data
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("newform");
var dataS = ss.getSheetByName("newdb");
var dataBaseNameColIndex = 1;
var formDataRange = ['E2:E97'];
var ui = SpreadsheetApp.getUi();
var response = ui.alert(
'Save Data ?',
ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
for (var i = 2; i < formDataRange.length; i++) {
var value = formS.getRange(formDataRange[i], 5).getValue();
dataS.getRange(dataS.getLastRow() + 1).setValue(value);
console.log(saveData);
}
}
}
Instead of trying to copy and paste data cell-by-cell, you may want to do it in one go. That would be more efficient.
So instead of:
var value = formS.getRange(formDataRange[i], 5).getValue();
you could use the following without the for loop:
var allValues = formS.getRange('E2:E97').getValues();
And then post the data to the new range like this:
dataS.getRange(dataS.getLastRow() + 1, 2, allValues.length, allValues[0].length).setValues(value);
I am assuming you want to paste the data into column 2 onwards. Adjust the 2nd value in getRange() above accordingly.
The 3rd and 4th values in getRange() above are the number of rows and columns to pasts.
allValues.length is the number of rows of data. In this case it would be 95.
and allValues[0].length would the number of columns in the top row of the data copied. And in this case it should be one.
I am suggesting this way as you won't need to keep fiddling with the number of rows and columns if you change the copy range dimensions later on.
PS:
dataS.getRange(dataS.getLastRow() + 1).setValue(value);
is missing the column number in getRange()

Google Script GetRange with Filter based on values in a column AND select only certain columns

I have a dataset of 35 columns and 300 rows. I want to get the range that contains rows only for certain values in column 30 (names). The name for which to filter the data is based on the report file cell B6 in the report sheet that is active. So far I tried this:
var report = SpreadsheetApp.getActiveSpreadsheet();
var tsheet = report.getSheetByName("Transactions");
var areport = SpreadsheetApp.getActiveSheet();
var agent = areport.getRange('B6').getValues();
var criteria = SpreadsheetApp.newFilterCriteria().whenTextEqualTo(agent).build();
var trange = tsheet.getRange().createFilter().setColumnFilterCriteria(30, criteria); // ERROR
var tdata = trange.getValues();
I receive an error Exception: The parameters () don't match the method signature for SpreadsheetApp.Sheet.getRange.
The second part, I only want to get several columns, 5,6,7, 13, 15. I can't create another filter with the Spreadsheet app, so is the only way to make an array and filter out the needed data from there? I'm just trying to think ahead and reduce the amount of calculations.
Try with filter():
var report = SpreadsheetApp.getActiveSpreadsheet();
var tsheet = report.getSheetByName("Transactions");
var areport = SpreadsheetApp.getActiveSheet();
var agent = areport.getRange('B6').getValue();
var data = tsheet.getRange('A1:AI300').getValues();
var tdata = data.filter(function (row) {
return row[29] == agent && row[5] == 'Closed' ; // starts from 0, column A is 0.
});
To select particular columns from tdata do:
var cr_data = getCols(tdata,[5,6,7, 13, 15]);
where getCols() is defined as follows:
function getCols(arr,cols) {
return arr.map(row =>
row.filter((_,i) => cols.includes(++i)))
}
and finally you can copy cr_data to a particular place/sheet like that:
sheet.getRange(1,1,cr_data.length,cr_data[0].length).setValues(cr_data);
Regarding the second part of your question I would like to redirect you to this post:
Best method to extract selected columns from 2d array in apps script

Need help creating an array of objects in Javascript

I have two Sheets
A "MEMBER" Sheet - 600 rows.
A "MODERATOR" Sheet - 13 rows (this is a subset of the "MEMBER" sheet)
Both sheets share a "Member Key" column - all unique values to identify the members.
I want to create a function that does the following
"...For every row in the MEMBER sheet..."
"...Compare the Member Key column in the MODERATOR sheet."
"If the Member Key column matches..."
"...pull values from that specific row in the MEMBER sheet..."
"and copy them into the MODERATOR sheet row with the matching KEY column"
The code is supposed to update the Moderator record with information from its correspondence Member record
the code I wrote for it is below.
The code runs, but the values don't update. I assume it's related to the for loop I applied. I'm hoping to resolve this issue by using object functions in the arrays, but I've had diffiuclty applying them in my code.
Grateful for any feedback on my question or code in general
function Moderator_Update() {
var Metrics = SpreadsheetApp.openById("10Wl1B4AtdLHJXBbLbMQbSdtRyAb61biCWYpOQEEywIY"); // METRICS spreadsheet
var Data = SpreadsheetApp.getActiveSpreadsheet(); // DATA spreadsheet
var ModsDataSheet = Data.getSheetByName("The Moderator_Numbers"); // DATA "Member" sheet
var ModsAllValues = ModsDataSheet.getRange(2, 1, ModsDataSheet.getLastRow()-1,ModsDataSheet.getLastColumn()).getValues(); //All values in DATA "Members" sheet
var MembersDataSheet = Data.getSheetByName("The Member_Numbers") // DATA "Moderator" sheet
var MembersAllValues = MembersDataSheet.getRange(2, 1, MembersDataSheet.getLastRow()-1,MembersDataSheet.getLastColumn()).getValues();
for(var MO = ModsAllValues.length-1;MO>=0;MO--) // for each row in the "Moderator" sheet...
{
for(var M = MembersAllValues.length-1;M>=0;M--) // for each row in the "Member" sheet...
{
if (MembersAllValues[M][17] == ModsAllValues[MO][27]) // If Member KEY matches Moderator Key.
{
// delcare the following variables with values from the Member Sheet
var Mod_MemberName = MembersAllValues[M][0];
var Mod_MemberWebPage = MembersAllValues[M][2];
var Mod_MemberTag = MembersAllValues[M][4];
var Mod_MemberFirstName = MembersAllValues[M][5];
var Mod_MemberLastName = MembersAllValues[M][6];
var Mod_MemberEmail = MembersAllValues[M][7];
// assign those values to the following cells in the Moderator Sheet
ModsAllValues[MO][0] = Mod_MemberName;
ModsAllValues[MO][2] = Mod_MemberWebPage;
ModsAllValues[MO][5] = Mod_MemberTag;
ModsAllValues[MO][6] = Mod_MemberFirstName;
ModsAllValues[MO][7] = Mod_MemberLastName;
ModsAllValues[MO][8] = Mod_MemberEmail;
}
}
}
var ModDestinationRange = ModsDataSheet.getRange(2, 1, ModsAllValues.length, ModsAllValues[0].length);
ModDestinationRange.setValues(ModsAllValues);
}
Moderator Sheet
Member Sheet
Try this:
It's not fastest possible solution but this way you don't have to worry about the effects of overwriting values that are not a part of the update.
function Moderator_Update() {
var Data=SpreadsheetApp.getActiveSpreadsheet();
var mdsh=Data.getSheetByName("The Moderator_Numbers");
var mdvA=mdsh.getRange(2,1,mdsh.getLastRow()-1,mdsh.getLastColumn()).getValues();
var mrsh=Data.getSheetByName("The Member_Numbers");
var mrvA=mrsh.getRange(2,1,mrsh.getLastRow()-1,mrsh.getLastColumn()).getValues();
var mdkeyA=mdsh.getRange(2,28,mdsh.getLastRow()-1,mdsh.getLastColumn()).getValues().map(function(r){return r[0];});
for(var j=0;j<mrvA.length;j++) {
var i=mdkeyA.indexOf(mrvA[j][17]);
if(i>-1){
mdsh.getRange(i+2,1).setValue(mrvA[j][0]);
mdsh.getRange(i+2,3).setValue(mrvA[j][2]);
mdsh.getRange(i+2,6).setValue(mrvA[j][4]);
mdsh.getRange(i+2,7).setValue(mrvA[j][5]);
mdsh.getRange(i+2,8).setValue(mrvA[j][6]);
mdsh.getRange(i+2,9).setValue(mrvA[j][7]);
}
}
}

Updating a sheet with a value if strings match on two sheets

Sheet 2 has all the items and their statuses, while Sheet 1 has only some of the items from Sheet 2. I want to be able to see every time an item mentioned on Sheet 1 is listed as having a status update, i.e. e date, on Sheet 2.
Here's what I have so far, but having trouble calling the right range to work with. Is there a simpler way to do what I want to do?
function statusupdate() {
var activesht = SpreadsheetApp.getActiveSpreadsheet();
var statussht = activesht.getSheetByName("Sheet 2"); //get sheet on which status update occurs
var statusrng1 = statussht.getRangeByName('B');
var statusrng2 = statussht.getRangeByName('C');
var mainsht = activesht.getSheetByName("Sheet 1"); //get sheet where you show a specific thing has been updated, if that thing mentioned here.
var mainrng = mainsht.getRangeByName('F');
if (statusrng1 == mainrng) {
var date = statusrng2.getValue();
var daterng = mainrng.getRangeByName('E');
daterng.setValues(date);
}
}
Spreadsheet formula
You can have the rows in one sheet follow those in another without using a script. For example, say we have a sheet named Items that contains one row for every item we carry, with the item number in the first column.
We can use VLOOKUP() to search for the row containing info about individual items, and select specific columns from it.
For example, this formula would be used in B2, and could be copied to other cells in our sheet:
=VLOOKUP($A2,Items!$A$2:$C$7,COLUMN(),false)
Script
There are a few issues with your script.
.getRangeByName('B') - This method gets a named range. Given the name, I suspect you mean to get column B, and NOT a named range. If that's the case, you could use this instead:
var statusrng1 = statussht.getRange('B:B');
In A1Notation, the range B:B is the entire column B.
You intend to copy values, so there is another step required beyond identifying ranges; you need to first read the values from a range, and then later write them to a different range. For that, you need to use methods like getValues() and setValues().
Here's an updated version of your script, adapted to the example spreadsheet described above.
function statusupdate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get sheet on which status update occurs
var statusSheet = ss.getSheetByName("Items");
var statusRange = statusSheet.getDataRange();
var statusData = statusRange.getValues();
//get sheet where you show a specific thing has been updated, if that thing mentioned here.
var trackingSheet = ss.getSheetByName("Tracking");
var trackingRange = trackingSheet.getDataRange();
var trackingData = trackingRange.getValues();
// Loop over all rows in the Tracking sheet to update from the Items sheet
// Start with row=1, because row 0 contains headers
for (var row=1; row<trackingData.length; row++) {
var item = trackingData[row][0];
if (item == '') continue; // skip rows without item #
// Look for item in Items sheet
var statusRow = null;
for (var sRow=1; sRow<statusData.length; sRow++) {
if (statusData[sRow][0] == item) {
// Found our match, grab that row
statusRow = statusData[sRow];
break;
}
}
// If we found a matching row, copy the status
if (statusRow) {
// Customize this depending on how your sheets are organized
trackingData[row][1] = statusRow[1];
trackingData[row][2] = statusRow[2];
}
}
// All values have been copied to trackingData, now write to sheet
trackingRange.setValues(trackingData);
}

Categories