2017-05-05 63 views
1

我尝试创建的程序有一个小问题。它应该计算出将能力带到一定水平需要多少exp。我还没有得到那么多,因为我在CalculateCost函数结束时返回时遇到了问题。我试图将一个字符串数组传递给名为CalculateCost的函数,但是,这不起作用,所以我尝试将所有值连接到单个字符串中。这也没有奏效。我知道我试图返回的变量不是null,因为我非常频繁地使用ui.alert()来检查变量的值。任何帮助将不胜感激。 以下是有问题的Google表格。 https://docs.google.com/spreadsheets/d/1Xo_uppFDI_C65EVi-TZ1iHTseK-Fg1izyCRkeTKaV9k/edit?usp=sharing 有问题的脚本被称为能力价格计算器。即使变量包含数据,Google脚本函数也会返回null

回答

1

看来问题是功能CalculateCost没有返回什么...

function CalculateCost(abilityValues,index,level) { 
 
    var ui = SpreadsheetApp.getUi();         
 
    var ss = SpreadsheetApp.getActive();       
 
    var sheet = ss.getActiveSheet();         
 
    ui.alert("CalculateCost"); 
 
    FindRequirements(abilityValues,index,sheet,level); 
 
    // current function returns nothing! 
 
} 
 

 
/* You may have thought, "oh but CalculateCost() already does the return, 
 
so there's no need". However, when we call a function that contains 
 
a return, it's as though we just paste the value that it returns. 
 
Plus, there was no command (alert, prompt, etc) at the end of your 
 
non-return-function, which made no sense. It made no sense to Google 
 
at least, so it was... undefined. */

所以我改成了

function CalculateCost(abilityValues,index,level) { 
 
    var ui = SpreadsheetApp.getUi();         
 
    var ss = SpreadsheetApp.getActive();        
 
    var sheet = ss.getActiveSheet();         
 
    ui.alert("CalculateCost"); 
 
    var reqs = FindRequirements(abilityValues,index,sheet,level); // notice the reqs var 
 
    return reqs; // this line was missing 
 
}

此外,我在排除代码故障时遇到了一些问题:我的主要问题是,当我们点击菜单中的CalculateCost时,通常会调用CalculateCost()函数,但实际上我们调用了InputValues()函数。当自定义函数没有绑定到相同的菜单项时,很容易迷路,所以稍后你可能会改变它。

请问,如果我的回答帮助了你的代码,你能否让我知道?

+1

所以我忘了第二次返回变量?这让我感觉真的很愚蠢。感谢您指出这一点,它确实有帮助。 –

+0

嘿,这表明你正在努力研究这个代码,我知道这感觉。保持它,它会完全按照你的要求进行;) –

+0

如果解决了这个问题,我建议你将我的答案标记为“已接受”。周末愉快! –

相关问题