lets put that mouth to work.
I am working on a project. Google sheets and app script.
I will be needing you to write a script for me to exactly kick off what I want.
now the script has already been provided by your friend GPT3 but its not accurate enough.
ill explain you what I want and then will share the existing script.
your job is to modify the script with maximum accuracy
Assistant
Absolutely! Please go ahead and explain exactly what you want to achieve with your Google Sheets and Apps Script project. Once you share the existing script and your requirements, I will help modify it with maximum accuracy.
User
Assistant
there is one google sheet "Extracted POs" that will contain columns : SR. NO, ITEM DESCRIPTION(PO number suffixed in the end like PO 143), INVOICE RATE, PO RATE, CHECK.
i will populate the first 3 columns.
there is a second google sheet "SPC POs" that will original PO details like PO NUMBER ITEM DESCRIPTION QUANTITY RATE.
we're trying to run a code that will extract item description and PO number in front of the item description from Sheet 1, and will extract the corresponding rate for that item that is given in sheet 2. the rate from sheet 2 will be extracted in column 4 of sheet 1, then column 5 "check" will check if rate both rates are same or not.
the challenge we have encountered so far was item names in both sheets being slightly different formatwise.
example: sheet 1 has an item "1600/5A CT 15VA - CLASS 1.0 - RESIN CAST
PO 143"
same item in sheet 2 goes like "1600/5A, CL-1.0, 15VA, RECTANGULAR(ID-130/55MM)"
the code must be able to identify this correctly everytime and fetch the correct rate.
for (let i = 1; i < draftData.length; i++) {
const itemDesc = draftData[i][1];
const inputRate = draftData[i][2];
code
if (!itemDesc || inputRate === "") continue;
// Extract PO number (assumed at end, e.g. "... PO-143")
const match = itemDesc.match(/PO-\d+$/i);
if (!match) {
draftSheet.getRange(i + 1, 4).setValue("N/A");
draftSheet.getRange(i + 1, 5).setValue("PO NOT FOUND");
continue;
}
const poNumber = match[0].toUpperCase();
// Try to match within the relevant PO block only
let inRelevantBlock = false;
let foundRate = null;
for (let j = 0; j < poData.length; j++) {
const poEntry = poData[j][0];
if (typeof poEntry === "string" && poEntry.toUpperCase() === poNumber) {
inRelevantBlock = true;
} else if (inRelevantBlock && typeof poEntry === "string" && poEntry.toUpperCase().startsWith("PO-") && poEntry.toUpperCase() !== poNumber) {
// Exited the relevant PO block
break;
}
if (inRelevantBlock) {
const poItem = poData[j][1];
const poRate = poData[j][3];
if (poItem && typeof poItem === "string" && looseMatch(itemDesc, poItem)) {
foundRate = poRate;
break;
}
}
}
// Fill columns D and E
if (foundRate !== null) {
draftSheet.getRange(i + 1, 4).setValue(foundRate); // PO Rate
const check = (Number(inputRate) === Number(foundRate)) ? "MATCH" : "MISMATCH";
draftSheet.getRange(i + 1, 5).setValue(check);
} else {
draftSheet.getRange(i + 1, 4).setValue("N/A");
draftSheet.getRange(i + 1, 5).setValue("ITEM NOT FOUND");
}
}
}
// Loose match helper
function looseMatch(desc1, desc2) {
// Remove PO number from desc1
const cleanDesc1 = desc1.replace(/PO-\d+$/i, "").trim().toLowerCase();
const cleanDesc2 = desc2.trim().toLowerCase();