How to search and match if a sheet name from a cell value in another sheet in google apps script and copy cells

huangapple go评论74阅读模式
英文:

How to search and match if a sheet name from a cell value in another sheet in google apps script and copy cells

问题

以下是代码的中文翻译部分:

function updateQuotationPrice() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var jobSheet = ss.getSheetByName('JOBS');
  var productSheet = SpreadsheetApp.openById("1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU").getSheetByName('PRODUCTS');
  var quotationSheet = SpreadsheetApp.openById("10tb0zE_8i849T-hL6mU-Pw_4V-aMzNZJGNOFYG1F3qk");
  var sheetNames = [];
  var sheets = quotationSheet.getSheets();
  sheets.forEach(function (sheet) {
    sheetNames.push(sheet.getName());
  });
  
  var objEuro = productSheet.getRange("A2:D" + productSheet.getLastRow()).getValues().reduce((o, r) => (o[r[0]] = r[3], o), {});
  var objSterling = productSheet.getRange("A2:D" + productSheet.getLastRow()).getValues().reduce((o, r) => (o[r[0]] = r[2], o), {});
  var objQuotationEuro = sheets.getRange("A2:D" + sheets.getLastRow()).getValues().reduce((o, r) => (o[r[0]] = r[3], o), {});
  var objQuotationSterling = sheets.getRange("A2:D" + sheets.getLastRow()).getValues().reduce((o, r) => (o[r[0]] = r[2], o), {});
  
  var range = jobSheet.getRange("O2:AK" + jobSheet.getLastRow());
  var euroValues = range.getValues().map(r => [objEuro[r[0]] || null]);
  var sterlingValues = range.getValues().map(r => [objSterling[r[0]] || null]);
  var quotationEuroValues = range.getValues().map(r => [objQuotationEuro[r[0]] || null]);
  var quotationSterlingValues = range.getValues().map(r => [objQuotationSterling[r[0]] || null]);
  
  var vs = jobSheet.getRange("O2:R" + jobSheet.getLastRow()).getValues();
  
  vs.forEach((r) => {
    if (r[0] == "EURO" && r[1].match(/^A/i) && sheets != r[3]) {
      range.offset(0, 2, euroValues.length, 1).setValues(euroValues);
    }
    
    if (r[0] == "STERLING" && r[1].match(/^A/i) && sheets != r[3]) {
      range.offset(0, 2, sterlingValues.length, 1).setValues(sterlingValues);
    }
    
    if (r[0] == "EURO" && r[1].match(/^A/i) && sheets == r[3]) {
      range.offset(0, 2, quotationValues.length, 1).setValues(quotationEuroValues);
    }
    
    if (r[0] == "STERLING" && r[1].match(/^A/i) && sheets == r[3]) {
      range.offset(0, 2, quotationValues.length, 1).setValues(quotationSterlingValues);
    }
  });
}

请注意,代码中的翻译可能不是百分之百准确,具体的代码逻辑可能需要进一步验证和调整。

英文:

I have three sheets, in the JOBS sheet if the user email address matches a sheet name in the QUOTATIONS workbook then the price is taken from that sheet for the specified product in column P. If no sheet name is found then the price for that product is taking from the PRODUCTS sheet. The price also depends on the currency which is specified in the JOBS sheet.
A sample of the expected results can be viewed in the JOBS sheet in column Q at https://docs.google.com/spreadsheets/d/1up7cUvQqL-LcA7EeuM65B0zjcRT46LGwN2x3C_4j0bY/edit?usp=sharing
If the user has a quotation and the email matches a sheet name then the price comes from QUOTATIONS workbook at
https://docs.google.com/spreadsheets/d/10tb0zE_8i849T-hL6mU-Pw_4V-aMzNZJGNOFYG1F3qk/edit?usp=sharing
If the user has no quotation and there is no matching email address the price comes from the products sheet
https://docs.google.com/spreadsheets/d/1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU/edit?usp=sharing

I have the following script which does match the price between the products sheet and the jobs sheet but unable to have it also check if a sheet name exists and it matches the email address in the jobs sheet and then gets the price from that sheet. Assistance is appreciated, thanks

function updateQuotationPrice() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobSheet = ss.getSheetByName('JOBS');
var productSheet = SpreadsheetApp.openById("1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU").getSheetByName('PRODUCTS');
var quotationSheet = SpreadsheetApp.openById("10tb0zE_8i849T-hL6mU-Pw_4V-aMzNZJGNOFYG1F3qk");
var sheetNames = []
var sheets = quotationSheet.getSheets()//.forEach(function(sheet){sheets[sheet.getName()]=sheet;});
sheets.forEach(function (sheet) {
sheetNames.push(sheet.getName());
console.log(sheets)
});
var objEuro = productSheet.getRange("A2:D" + productSheet.getLastRow()).getValues().reduce((o, r) =>  (o[r[0]] = r[3], o), {});
var objSterling = productSheet.getRange("A2:D" + productSheet.getLastRow()).getValues().reduce((o, r) =>  (o[r[0]] = r[2], o), {});
var objQuotationEuro = sheets.getRange("A2:D" + sheets.getLastRow()).getValues().reduce((o, r) =>  (o[r[0]] = r[3], o), {}); 
var objQuotationSterling = sheets.getRange("A2:D" + sheets.getLastRow()).getValues().reduce((o, r) =>  (o[r[0]] = r[2], o), {});
var range = jobSheet.getRange("O2:AK" + jobSheet.getLastRow());
var euroValues = range.getValues().map(r => [objEuro[r[0]] || null]);
var sterlingValues = range.getValues().map(r => [objSterling[r[0]] || null]);
var quotationEuroValues = range.getValues().map(r => [objQuotationEuro[r[0]] || null]);
var quotationSterlingValues = range.getValues().map(r => [objQuotationSterling[r[0]] || null]);
var vs = jobSheet.getRange("O2:R"+ jobSheet.getLastRow()).getValues();
vs.forEach((r) => {  
if(r[0] == "EURO" && r[1].match(/^A/i) && sheets != r[3]  ){
range.offset(0, 2, euroValues.length, 1).setValues(euroValues)};
if(r[0] == "STERLING" && r[1].match(/^A/i)  && sheets != r[3]){
range.offset(0, 2, sterlingValues.length, 1).setValues(sterlingValues)}
if(r[0] == "EURO" && r[1].match(/^A/i) && sheets == r[3] ){
range.offset(0, 2, quotationValues.length, 1).setValues(quotationEuroValues)}
if(r[0] == "STERLING" && r[1].match(/^A/i) && sheets == r[3] ){
range.offset(0, 2, quotationValues.length, 1).setValues(quotationSterlingValues)}
});
}

答案1

得分: 2

I have created another approach wherein I still used the forEach() function to process the data. However, I used the filter() and includes() function to help in the data matching process.

你提出了另一种方法,在其中仍然使用了 forEach() 函数来处理数据。不过,我使用了 filter()includes() 函数来帮助数据匹配过程。

You may use the following script as the basis for your script.

你可以将以下脚本作为你的脚本的基础使用。

function updateQuotationPrice() {
  var ss1 = SpreadsheetApp.openById("User Update Sheet ID"); // 用户更新
  var ss2 = SpreadsheetApp.openById("Quotation Sheet ID"); // 报价
  var ss3 = SpreadsheetApp.openById("Projects Sheet ID"); // 项目

  // 从用户更新中提取数据
  var lr1 = ss1.getSheetByName("Jobs").getLastRow();
  var data1 = ss1.getSheetByName("Jobs").getRange(2, 13, lr1 - 1, 4).getValues();

  // 从报价电子表格中提取数据
  var sheetNames2 = [];
  ss2.getSheets().forEach(x => sheetNames2.push(x.getName()));

  // 处理数据 1
  var output = [];
  data1.forEach(x => {
    if (sheetNames2.includes(x[0]) && ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y.includes(x[3])).length > 0) { // 在这里添加了新的条件
      if (x[2] == "EURO") {
        output.push([ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y includes(x[3]))[0][3]]);
      } else if (x[2] == "STERLING") {
        output.push([ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y.includes(x[3]))[0][2]]);
      }
    } else {
      if (x[2] == "EURO") {
        output.push([ss3.getSheetByName("Products").getDataRange().getValues().filter(y => y.includes(x[3]))[0][3]]);
      } else if (x[2] == "STERLING") {
        output.push([ss3.getSheetByName("Products").getDataRange().getValues().filter(y => y.includes(x[3]))[0][2]]);
      }
    }
  });

  // 输出
  ss1.getSheetByName("Jobs").getRange(2, 17, lr1 - 1, 1).setValues(output);
}

References

参考资料

英文:

Suggestion

I have created another approach wherein I still used the forEach() function to process the data. However, I used the filter() and includes() function to help in the data matching process.

Script

You may use the following script as basis for your script.

function updateQuotationPrice() {
var ss1 = SpreadsheetApp.openById("User Update Sheet ID"); //User Update
var ss2 = SpreadsheetApp.openById("Quotation Sheet ID"); //Quotation
var ss3 = SpreadsheetApp.openById("Projects Sheet ID"); //Projects
//Extract data from User Update
var lr1 = ss1.getSheetByName("Jobs").getLastRow();
var data1 = ss1.getSheetByName("Jobs").getRange(2, 13, lr1 - 1, 4).getValues();
//Extract data from Quotation Spreadsheet
var sheetNames2 = [];
ss2.getSheets().forEach(x => sheetNames2.push(x.getName()));
//Process data 1
var output = [];
data1.forEach(x => {
if (sheetNames2.includes(x[0]) && ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y.includes(x[3])).length > 0) { // added new condition here
if (x[2] == "EURO") {
output.push([ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y.includes(x[3]))[0][3]]);
}
else if (x[2] == "STERLING") {
output.push([ss2.getSheetByName(x[0]).getDataRange().getValues().filter(y => y.includes(x[3]))[0][2]]);
}
}
else {
if (x[2] == "EURO") {
output.push([ss3.getSheetByName("Products").getDataRange().getValues().filter(y => y.includes(x[3]))[0][3]]);
}
else if (x[2] == "STERLING") {
output.push([ss3.getSheetByName("Products").getDataRange().getValues().filter(y => y.includes(x[3]))[0][2]]);
}
}
});
//Output
ss1.getSheetByName("Jobs").getRange(2, 17, lr1 - 1, 1).setValues(output);
}

Output

How to search and match if a sheet name from a cell value in another sheet in google apps script and copy cells

References

huangapple
  • 本文由 发表于 2023年2月10日 05:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定