简体   繁体   English

为什么我不断出错#NUM! 第一次运行该函数后,重新运行它时没有错误?

[英]Why I kept getting err #NUM! after running the function for the first time but then no error when I re-run it?

I'm trying to do some simple maths calculation on some values in some columns in gsheet.我正在尝试对 gsheet 中某些列中的某些值进行一些简单的数学计算。 Each value in each column is also populated from other sub functions prior to this function that kept giving me error #NUM!每列中的每个值也由此函数之前的其他子函数填充,这些子函数一直给我错误 #NUM! each time I run it but then it'll turn out to be ok with the correct expected output when I rerun it again.每次我运行它,但是当我再次运行它时,它会得到正确的预期输出。
样本数据

The logic is supposed to be as below:-逻辑应该如下: -
Nett Total Item = Gross Total Item - Total Subtract Item + (Total AddVal * (Gross Total Item - Total Subtract Item)) + (Total MoreVal * (Gross Total Item - Total Subtract Item)) Nett Total Item = Gross Total Item - Total Subtract Item + (Total AddVal * (Gross Total Item - Total Subtract Item)) + (Total MoreVal * (Gross Total Item - Total Subtract Item))

Below is my code :下面是我的代码:

const sheet=SpreadsheetApp.openById("1c0-vzYQA_9KVSiSgHUtS-3iBBGhTg3EcCZBm7rZDIpM").getSheetByName('mydata');
const lastrow=sheet.getLastRow();

function myFunction(){
  getGrossTotalItem(); //external fn to populate and setValue Gross Total Item in lastrow col AN
  getTotalSubtractItem(); //external fn to populate and setValue Total Subtract Item in lastrow col AV
  var TotalAddValrate=getTotalVal()[0]; //external fn to return % 1st val in the array and display in lastrow col AW
  var TotalMoreValrate=getTotalVal()[1]; //external fn to return % 2nd val in the array and display in lastrow col AX

  var grossTotalItem=sheet.getRange(lastrow,40).getValue();
  var totalSubtractItem=sheet.getRange(lastrow,48).getValue();
  var diff=grossTotalItem - totalSubtractItem;
  var TotalAddVal = TotalAddValrate * diff;
  var TotalMoreVal= TotalMoreValrate * diff;
  var nettTotalItem = diff + TotalAddVal + TotalMoreVal;
  sheet.getRange(lastrow,51).setValue(nettTotalItem);
}

It should be very straightforward process though but I don't understand why I keep getting the err on Nett Total Item until I run it the second time.不过,这应该是一个非常简单的过程,但我不明白为什么我在第二次运行之前一直在 Nett Total Item 上出错。
But all other values have no issue.但所有其他值都没有问题。 Perhaps the process time to return the values for nettTotalItem formula to run is insufficient or something?也许返回 nettTotalItem 公式运行的值的处理时间不足或什么?
Appreciate if anyone can correct my code to calculate a better way or more efficient with no error.感谢是否有人可以更正我的代码以计算更好的方法或更有效而没有错误。

I used Logger.log to capture the result as per advised by @Mario.根据@Mario 的建议,我使用 Logger.log 来捕获结果。 it does shows that the desired output is there, but I just don't understand why is it not printed in the cell range as coded.它确实表明所需的输出在那里,但我只是不明白为什么它没有按照编码打印在单元格范围内。
日志截图

After tracing through the logs, I found the culprit was sets of scripts to populate the TotalAddValrate value in external fn getTotalVal() .在跟踪日志后,我发现罪魁祸首是在外部 fn getTotalVal()填充TotalAddValrate值的脚本集。 Below was my original code :下面是我的原始代码:

function getTotalVal() {
  const paramdata=paramsheet.getDataRange().getValues();
  const data=sheet.getDataRange().getValues();
  
  var TotalAddValrate=data[lastrow-1][48];
  if(TotalAddValrate==''){
  var TotalAddValheader=data[0][48];
  var paramrow=paramdata.findIndex(TotalAddValrate=> {return TotalAddValrate[0] == TotalAddValheader }) +1;
  TotalAddValrate=sheet.getRange(lastrow,49).setValue(paramdata[paramrow-1][1]);
  }

  var TotalMoreValrate=data[lastrow-1][49];
  if(TotalMoreValrate==''){
  var TotalMoreValheader=data[0][49];
  var paramrow=paramdata.findIndex(TotalMoreValrate=> {return TotalMoreValrate[0] == TotalMoreValheader }) +1;
  TotalMoreValrate=sheet.getRange(lastrow,50).setValue(paramdata[paramrow-1][1]);
  }
  var allvals=[TotalAddValrate,TotalMoreValrate];
  return allvals;
}

The weird thing is the original sets of scripts to populate TotalAddValrate value was identical with the original sets of scripts to populate TotalMoreValrate value but somehow I don't see any issue on TotalMoreValrate scripts so I retain the code as it is.奇怪的是原来的脚本组来填充TotalAddValrate值与原件一致的脚本组来填充TotalMoreValrate值但不知何故,我看不出有任何问题TotalMoreValrate脚本,所以我保留了代码,因为它是。 As for the TotalAddValrate scripts, I changed it to below and I finally got what I expected:-至于TotalAddValrate脚本,我将其更改为以下内容,最终得到了预期的结果:-

  var TotalAddValrate=ordersheet.getRange(lastrow,49).getValue();
  if(TotalAddValrate==''){
  var TotalAddValheader=sheet.getRange(1,49).getValue();
  var paramrow=paramdata.findIndex(TotalAddValrate=> {return TotalAddValrate[0] == TotalAddValheader }) +1;
  sheet.getRange(lastrow,49).setValue(paramsheet.getRange(paramrow,2).getValue());
  TotalAddValrate=ordersheet.getRange(lastrow,49).getValue();
  }

Nevertheless, I would appreciate it so much if anyone can happily share any better and faster solution to get the same desired final output as per logic mentioned in my initial question.尽管如此,如果有人能愉快地分享任何更好更快的解决方案,以获得与我最初问题中提到的逻辑相同的所需最终输出,我将不胜感激。 The one I've done is purely based on what I learned so far on GAS and I'm just starting about 2months ago.我所做的完全基于我迄今为止在 GAS 上学到的知识,而且我大约在 2 个月前才开始使用。

暂无
暂无

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

相关问题 当我在其中调用的集合更新时,为什么我的助手没有重新运行? - Why my helper doesn't re-run when the collection I call inside it is updated? 为什么我在系统中第一次运行 react js 项目时收到此错误消息? - Why I am getting this error message when I run react js project first time in my system? 为什么我在运行时收到此错误:npm run build? - Why am I getting this error when running: npm run build? jQuery运行函数等待X次,然后重新运行 - JQuery run function wait X time and then re-run 我保存时测试失败(在测试脚本配置中使用`--watch`)但如果我手动重新运行它们会通过? - Tests fail when I save (using `--watch` in test script config) but if I re-run manually they pass? 为什么我在下载文件时收到错误“错误 [ERR_STREAM_WRITE_AFTER_END]: write after end”? - Why am I getting the error “Error [ERR_STREAM_WRITE_AFTER_END]: write after end” when downloading a file? 当文件夹的内容发生变化时,如何重新运行 Javascript 文件? - How do I re-run a Javascript-file when the content of a folder changes? 为什么在运行 npm 运行启动时总是出现此错误? 这是 React 的导入错误 - Why do I keep getting this error when running npm run start? It's an import error on React localStorage之后重新运行脚本 - Re-run script after localStorage 满足条件后如何重新运行事件功能? - How to re-run an event function after a condition has been met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM