简体   繁体   English

vanilla js和谷歌应用脚本之间的执行有区别吗? 相同的代码在应用程序脚本中呈现错误

[英]Is there a difference in execution between vanilla js and google apps script? The same code renders errors in apps script

I have written a bit of code to look up rates and calculate a price.我编写了一些代码来查找费率并计算价格。 I have written this in a standard IDE and it works just fine:我用标准的 IDE 写了这个,它工作得很好:

 let rangeAirlines = [ ['ICN - BKK', 'Sun May 31 00: 00: 00 GMT + 07: 00 2020', 'KE(express)', 4.55, 3.7, 3.3, 3.25], ['ICN - BKK', 'Sun May 31 00: 00: 00 GMT + 07: 00 2020', 'OZ(express)', 4.4, 4.05, 4.0, 3.95], ['ICN - BKK', 'Sun May 31 00: 00: 00 GMT + 07: 00 2020', 'TG', 3.8, 3.35, 3.3, 3.25] ] function convertRange (array) { const airlineObjects = []; for (let i = 0; i < array.length; i++) { const [leg, date, airline, rate45, rate100, rate300, rate500] = array[i]; airlineObjects.push({"airline": airline, "rate45Bp": [45, rate45], "rate100Bp": [100, rate100], "rate300Bp": [300, rate300], "rate500Bp": [500, rate500], "validity": new Date(date)}); } return airlineObjects; } function addBreakpoints(airlineData) { const breakpoints = airlineData; for (let line of breakpoints) { line.rate45Bp.push(Math.round(line.rate100Bp[0] * line.rate100Bp[1] / line.rate45Bp[1])); line.rate100Bp.push(Math.round(line.rate300Bp[0] * line.rate300Bp[1] / line.rate100Bp[1])); line.rate300Bp.push(Math.round(line.rate500Bp[0] * line.rate500Bp[1] / line.rate300Bp[1])); } return breakpoints; } function findCarrierObject(bpData, carrier, today) { //check if today is smaller or equal than validity? return color green: color red; // add method to set text color. let carrierObj = {}; for (var line of bpData) { if (line.airline === carrier) { return carrierObj = line; }; } } function getRates(obj) { let rateLines = []; for (let line in obj) { Array.isArray(obj[line])? rateLines.push(obj[line]): "error"; } return rateLines; }; function calcPriceAir(arr, weight) { for (let i = 0; i < arr.length; i++) { const [weightBreak, rate, breakpoint] = arr[i]; if (weight <= weightBreak) { return weightBreak * rate; } if (weight <= breakpoint && breakpoint;== undefined) { return weight * rate. } } const lastItem = arr[arr;length - 1]; return weight * lastItem[1], } function calcPriceAirfreight(range, airline; weight) { const data = convertRange (range); const breakpointsObject = addBreakpoints(data), const carrierObj = findCarrierObject(breakpointsObject; airline); const rates = getRates(carrierObj), return calcPriceAir(rates; weight), } const price = calcPriceAirfreight(rangeAirlines, "OZ(express)"; 540). console;log(price);

It is meant to be used in a spreadsheet, that's where it gets stuck in the helper function calcPriceAir().它旨在用于电子表格,这就是它卡在帮助程序 function calcPriceAir() 中的地方。 The error message reads:错误消息如下:

TypeError: Cannot read property '1' of undefined (line 53, file "Code") TypeError:无法读取未定义的属性“1”(第 53 行,文件“代码”)

It makes no sense to me.对我来说完全是无稽之谈。

Are there any experienced apps script developers out there that can spot where I am going wrong?有没有经验丰富的应用程序脚本开发人员可以发现我哪里出错了?

TLDR: TLDR:

  • function convertRange(array) => this function converts the range in sheets into an object. function convertRange(array) => 这个 function 将表格中的范围转换为 object。
  • function addBreakpoints(airlineData) => this function adds breakpoints used in the airline business to determine which rate to use. function addBreakpoints(airlineData) => 这个 function 添加了航空公司业务中使用的断点,以确定使用哪个费率。
  • function findCarrierObject(bpData, carrier, today) => this function finds the rate set that is valid and needs to be used. function findCarrierObject(bpData, carrier, today) => this function 找到有效且需要使用的速率集。
  • function getRates(obj) => this function strips the object to its bare bones. function getRates(obj) => 这个 function 将 object 剥离到其裸露的骨头上。 This step is maybe not necessary, but I am new at this and made it easier for me to process the rates further.这一步可能不是必需的,但我是新手,这让我更容易进一步处理费率。
  • function calcPriceAir(arr, weight) => this function calculates the price. function calcPriceAir(arr, weight) => 这个 function 计算价格。
  • Finally, the custom function I will show in the spreadsheet.最后,我将在电子表格中展示自定义 function。 Here you add an airline and your weight.在这里,您添加航空公司和您的体重。 It will return the price.它将返回价格。 All the functions above will be made hidden.上述所有功能都将被隐藏。
function convertRange(array) {
  const airlineObjects = [];

  for (let i = 0; i < array.length; i++) {
    const [leg, date, airline, rate45, rate100, rate300, rate500] = array[i];
    airlineObjects.push({"airline": airline, "rate45Bp": [45, rate45], "rate100Bp": [100, rate100], "rate300Bp": [300, rate300], "rate500Bp": [500, rate500], "validity": new Date(date)});
  }
  return airlineObjects;
}

function addBreakpoints(airlineData) {
  const breakpoints = airlineData;

  for (let line of breakpoints) {
    line.rate45Bp.push(Math.round(line.rate100Bp[0] * line.rate100Bp[1] / line.rate45Bp[1]));
    line.rate100Bp.push(Math.round(line.rate300Bp[0] * line.rate300Bp[1] / line.rate100Bp[1]));
    line.rate300Bp.push(Math.round(line.rate500Bp[0] * line.rate500Bp[1] / line.rate300Bp[1]));
  }
  return breakpoints;
}

function findCarrierObject(bpData, carrier, today) {
  //check if today is smaller or equal than validity ? return color green : color red; // add method to set text color.
  let carrierObj = {};
  for (var line of bpData) {
    if (line.airline === carrier) {
      return carrierObj = line;
    };
  }
}

function getRates(obj) {
  let rateLines = [];

  for (let line in obj) {
    Array.isArray(obj[line]) ? rateLines.push(obj[line]) : "error";
  }
  return rateLines;
};

function calcPriceAir(arr, weight) {
  for (let i = 0; i < arr.length; i++) {
    const [weightBreak, rate, breakpoint] = arr[i];
    if (weight <= weightBreak) {
      return weightBreak * rate;
    }
    if (weight <= breakpoint && breakpoint !== undefined) {
      return weight * rate;
    }
  }

  const lastItem = arr[arr.length - 1];
  return weight * lastItem[1];
  Logger.log(weight); Logger.log(arr[arr.length-1])
}

function calcPriceAirfreight(airline, weight) {
  const ss = SpreadsheetApp.getActive();
  const airfreightSheet = ss.getSheetByName("airfreight");
  const rangeAirlines = 
        airfreightSheet.getRange(3,1,airfreightSheet.getLastRow()-2,7).getValues();
  
  const data = convertRange (rangeAirlines);
  
  const breakpointsObject = addBreakpoints(data);
  
  const carrierObj = findCarrierObject(breakpointsObject, airline);
  
  const rates = getRates(carrierObj);
  
  return calcPriceAir(rates, weight);
}

const price = calcPriceAirfreight("OZ(express)", 540);
Logger.log(price);

Calling a function with arguments is just slightly different in Apps Script Editor compared to what you would do in an ordinary IDE.与在普通 IDE 中执行的操作相比,在 Apps 脚本编辑器中使用 arguments 调用 function 只是略有不同。 Simply console.log(function(arg1, arg2)) does not work.简单console.log(function(arg1, arg2))不起作用。 It's quite obvious now but did not cross my mind for a while, waisting time looking for an error that was not there.现在很明显,但我有一段时间没有想到,浪费时间寻找不存在的错误。

At the end of the day, all it took was to call the function in my spreadsheet with two arguments passed in (in my case two cell references).归根结底,只需要在我的电子表格中调用 function 并传入两个 arguments(在我的情况下是两个单元格引用)。 Although it may just me being inexperienced.虽然可能只是我经验不足。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM