[英]Getting each cell value from a range of cells in google app script
I have the following script which pulls data from the products sheet from either r[3] if the currency is "EURO" in column O of the JOB sheet, or r[2] if the currency is "STERLING" in column O of the job sheet.我有以下脚本,如果工作表的 O 列中的货币为“欧元”,则从 r[3] 中的产品表中提取数据,如果工作表的 O 列中的货币为“STERLING”,则为 r[2]工作表。 The problem is that when the script runs it is not differentiating between the currencies according to each row in the job sheet.问题在于,当脚本运行时,它不会根据作业表中的每一行区分货币。 The script is reading the currency value in the last row and using that value to input the value from either r[2] or r[3] of the products sheet instead of reading the currency value from each of the rows.该脚本读取最后一行中的货币值,并使用该值从产品表的 r[2] 或 r[3] 输入值,而不是从每一行读取货币值。 Thank you for any help resolving this issue The job sheet can be viewed at https://docs.google.com/spreadsheets/d/1up7cUvQqL-LcA7EeuM65B0zjcRT46LGwN2x3C_4j0bY/edit?usp=sharing The products sheet can be viewed at https://docs.google.com/spreadsheets/d/1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU/edit?usp=sharing感谢您为解决此问题提供的任何帮助 可以在https://docs.google.com/spreadsheets/d/1up7cUvQqL-LcA7EeuM65B0zjcRT46LGwN2x3C_4j0bY/edit?usp=sharing查看工作表 可以在https://docs查看产品表.google.com/spreadsheets/d/1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU/edit?usp=sharing
function updatePrice() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobSheet = ss.getSheetByName('JOBS');
var productSheet = SpreadsheetApp.openById("1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU").getSheetByName('PRODUCTS');
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 range = jobSheet.getRange("P2:AK" + jobSheet.getLastRow());
var euroValues = range.getValues().map(r => [objEuro[r[0]] || null]);
var sterlingValues = range.getValues().map(r => [objSterling[r[0]] || null]);
var vs = jobSheet.getRange("O2:P"+ jobSheet.getLastRow()).getValues();
vs.forEach((r) => {
if(r[0] == "EURO" && r[1].match(/^A/i) ){
range.offset(0, 10, euroValues.length, 1).setValues(euroValues)};
if(r[0] == "STERLING" && r[1].match(/^A/i) ){
range.offset(0, 10, sterlingValues.length, 1).setValues(sterlingValues)}});
}
I'm not sure that you will consider this as an answer to your question.我不确定您是否会将此视为问题的答案。
I was unable to follow the map command for the objects ( var euroValues = range.getValues().map(r => [objEuro[r[0]] || null])
and var sterlingValues = range.getValues().map(r => [objSterling[r[0]] || null]);
), so I just wrote it old-school:我无法遵循对象的 map 命令( var euroValues = range.getValues().map(r => [objEuro[r[0]] || null])
和var sterlingValues = range.getValues().map(r => [objSterling[r[0]] || null]);
),所以我只是老派地写了它:
setValues
at the end of the script.在脚本末尾更新单个setValues
中的价格范围。function updatePrice() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobSheet = ss.getSheetByName('JOBS');
var productSheet = SpreadsheetApp.openById("1pt7YnN9fmoD4PE0o9oVPezK8Qz6ZmabyJbWXfdzFeMU").getSheetByName('PRODUCTS');
// get products and jobs
var products = productSheet.getRange("A2:D" + productSheet.getLastRow()).getValues()
var jobsRange = jobSheet.getRange("O2:AK" + jobSheet.getLastRow())
var jobs = jobsRange.getValues()
// get the list of products on the Jobs sheet and Product Codes on the products sheet
var jobProducts = jobs.map(function(e){return e[1]})
var prodCodes = products.map(function(e){return e[0]})
// find the indices of every Job in the Product array
var indices = jobProducts.map(
Map.prototype.get,
prodCodes.reduce((m, v, i) => m.set(v, i), new Map)
);
var priceArray = []
// for each job get the Products index and the Price and push the price onto an array
for (var i=0;i<jobProducts.length;i++){
// assign value depending whether currency = Sterling or Euro
if (jobs[i][0] === "STERLING"){
var productValue = products[indices[i]][2]
}
else if (jobs[i][0] === "EURO"){
var productValue = products[indices[i]][3]
}
priceArray.push([productValue])
}
jobsRange.offset(0, 12,jobs.length,1).setValues(priceArray)
SpreadsheetApp.flush
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.