Home > Blockchain >  Getting each cell value from a range of cells in google app script
Getting each cell value from a range of cells in google app script

Time:01-18

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. 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. 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

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)}});

}

CodePudding user response:

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:


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
}
  • Related