简体   繁体   English

从谷歌应用脚本中的一系列单元格中获取每个单元格值

[英]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]); ),所以我只是老派地写了它:


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.

相关问题 查找范围内值的匹配项并获取每个匹配项的第一个单元格和第一列值谷歌应用脚本 - Find matches to a value in a range and get first cell and first column values for each match google app script 为范围内的每个单元格添加值(Google 应用程序脚本) - Add value to each cell in range (Google apps script) Google Apps 脚本 - 获取一系列单元格而不是一个单元格 - Google Apps Script - Get a range of cells instead of one cell 我想在单元格范围内搜索文本并将值返回到单元格的左侧(Google App Script) - I want to search a text within a cell range and return the value to the left of the cell (Google App Script) 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets Google脚本-从单个单元格获取范围 - Google Script - Get Range from single cell 在谷歌应用脚​​本中获取“不正确的范围高度” - Getting “Incorrect range height” in google app script 从当前行单元格和 output 中减去两个值,将该值放入一个新的单元格行(谷歌脚本) - Subtract two values from current row cells and output that value into a new cell row (google script) Google App 脚本根据该单元格的字体颜色清除单元格值 - Google App script to clear cells values on the base of Font color of that cell Google Spreadsheet脚本修剪范围内每个单元格的前X个字符 - Google Spreadsheet Script Trim First X Characters Each Cell in Range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM