问题
I'm having difficulty trying to simultaneously compare multiple values from one sheet with multiple values with another sheet.
To explain below...
I'm using App Script (Javascript-based) for Google Sheet. I have two tables.
The first table is the PINPOINT table (500 rows, all unique values)
This table has a State, Area, City & Location and Key column.
The second table is the FEED table (2800 rows with some duplicates).
This tables which contain the values from PINPOINT'S State, Area, City & Location and Key columns
What i want is a code that says
"If a row from column CQ in the FEED sheet contains the Key Value from column P in the PINPOINT sheet
...but columns CR-CU in the FEED sheet row doesn't have the same values as column L-O in the PINPOINT table...
update column CR-CU the FEED sheet with the same combination of values as L-O in the PINPOINT table."
In other words,
" Check all FEED's rows for PINPOINT's Key Value...
...If PINPOINT & FEED Key columns match...
...but the PINPOINT & FEED rows don't match
use the PINPOINT row to make the FEED row match"
The problem I have is that I don't know how to simultaneously compare the values of the PINPOINTS 4 columns with the FEED's 4 columns. So i making a loop for each column to compare them one at a time.
Disclaimer: The 4 values need to be in their own columns for reasons too complicated to explain.
I pasted a reproducible sample of the code i have below, as well as the images. You should be able to plug in the code in Google App Script and see it work as long as you create two tables with data.
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.
Pinpoint Sheet Feed Sheet
function Pinpoint_Test() {
var Data = SpreadsheetApp.getActiveSpreadsheet(); // DATA spreadsheet
var PinpointDataSheet = Data.getSheetByName("The Pinpoints_Test") // DATA "Pinpoint" sheet
var PinpointAllValues = PinpointDataSheet.getRange(2, 1, PinpointDataSheet.getLastRow()-1,PinpointDataSheet.getLastColumn()).getValues();
var PinpointDataLastRow = PinpointDataSheet.getLastRow();
var FeedDataSheet = Data.getSheetByName("The Feed_Test") // DATA "Feed" sheet
var FeedAllValues = FeedDataSheet.getRange(2, 1, FeedDataSheet.getLastRow()-1,FeedDataSheet.getLastColumn()).getValues();
// Object to contain all Pinpoint column values
var StateObj = {};
var AreaObj = {};
var CityObj = {};
var SpotObj = {};
for(var P = PinpointAllValues.length-1;P>=0;P--) // for each row in the "Pinpoint" sheet...
{
StateObj[PinpointAllValues[P][15]] = PinpointAllValues[P][11]; // ...store State value
AreaObj[PinpointAllValues[P][15]] = PinpointAllValues[P][12]; // ...store Area value
CityObj[PinpointAllValues[P][15]] = PinpointAllValues[P][13]; // ...store City value
SpotObj[PinpointAllValues[P][15]] = PinpointAllValues[P][14]; // ...store Spot value
}
for(var F = FeedAllValues.length-1;F>=0;F--) // for each row in the "Feed" sheet...
{
var Feed_PinpointKey = FeedAllValues[F][94]; // Store Pinpoint Key.
// ...if FEED's State value dont match, update it with PINPOINT's value
if (StateObj[Feed_PinpointKey] != FeedAllValues[F][95])
{
FeedAllValues[F][95] = StateObj[Feed_PinpointKey];
}
// ...if FEED's Area value dont match, update it with PINPOINT's value
if (AreaObj[Feed_PinpointKey] != FeedAllValues[F][96])
{
FeedAllValues[F][96] = AreaObj[Feed_PinpointKey];
}
// ...if FEED's City value dont match, update it with PINPOINT's value
if (CityObj[Feed_PinpointKey] != FeedAllValues[F][97])
{
FeedAllValues[F][97] = CityObj[Feed_PinpointKey];
}
// ...if FEED's Spot value dont match, update it with PINPOINT's value
if (SpotObj[Feed_PinpointKey] != FeedAllValues[F][98])
{
FeedAllValues[F][98] = SpotObj[Feed_PinpointKey];
}
}
// declare the space to place updated values, then set it.
var FeedDestinationRange = FeedDataSheet.getRange(2, 1, FeedAllValues.length, FeedAllValues[0].length);
FeedDestinationRange.setValues(FeedAllValues);
}
来源:https://stackoverflow.com/questions/59870660/how-can-i-compare-multiple-google-sheet-values-using-google-apps-script