Home > database >  Chang Column Reference In Googleapp script
Chang Column Reference In Googleapp script

Time:11-24

I am very new to Google appscirpt and with the help and support of this community I am getting excellent solution to my queries. I had a question similar to one posted on below link but I want a very small modification in the same. I tried to get the solution on my query on different questions posted on this community but could not find a solution. Any help on above will be appreciated.

Link for similar question:

Vlookup with split text by Google Appscript

Code used in above link:

function onEdit(e) {
const sheetName = "Sheet3"; // This sheet name is from your Spreadsheet.
const { range, value, source } = e;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.getA1Notation() != "A1" || value != 
"Refresh") return;
const sheet1 = source.getSheetByName("Sheet1"); // This sheet name is from your 
Spreadsheet.
const sheet2 = source.getSheetByName("Sheet2"); // This sheet name is from your 
Spreadsheet.
const range1 = sheet1.getRange("A2:B"   sheet1.getLastRow());
const obj = sheet2.getRange(**"A2:B"**   sheet2.getLastRow()).getValues().reduce((o, 
[a, 
b]) => (o[a] = b, o), {});
const values = range1.getValues().map(([a, b]) => {
const temp = obj[a.split("*")[0]];
return [temp && !b.toString() ? temp : null];
});
range1.offset(0, 2, values.length, 1).setValues(values);
range.setValue(null);
}

In the above code "const obj = sheet2.getRange("A2:B"" is used to return the value of column B from sheet2, however, I want to use same code at a different place and want to return the value of column G in place of Column B i.e. my case range will be A2:G and return the value of column G.

Any help on above will be appreciated.

CodePudding user response:

I believe your goal is as follows.

  • You want to use the values of column "G" of "Sheet2" instead of column "B".
  • In this case, the values of column "A" are used for searching.

In this case, how about the following modification?

From:

const obj = sheet2.getRange(**"A2:B"**   sheet2.getLastRow()).getValues().reduce((o, 
[a, 
b]) => (o[a] = b, o), {});

To:

const obj = sheet2.getRange("A2:G"   sheet2.getLastRow()).getValues().reduce((o, [a,,,,,,g]) => (o[a] = g, o), {});
  • By this modification, the values of column "G" is used instead of the values of column "B".
  • Related