[英]Google App Script - How to use methods without defining their object
I am writing the script behind a spreadsheet that has lots of durations on it in the format of (##:##.##) (ex 12:43.76). 我在(##:##。##)(例如12:43.76)格式的电子表格后面编写脚本。 I need to write some code that converts this to just seconds.
我需要编写一些代码,将其转换为几秒钟。 I wrote code that did the opposite, made seconds into that format.
我写了相反的代码,使这种格式花费了几秒钟。 But when writing a custom formula for this, the .split method does not work.
但是,为此编写自定义公式时,.split方法不起作用。
function MTOS(input){
String(input);
if (typeof(input) != "string") {
Logger.log("Not a string")}
var array = input.split(":");
Logger.log('The original string is: "' + input + '"');
var min = Number(array[0]);
var sec = Number(array[1]);
Logger.log("min=" + min);
Logger.log("sec=" + sec);
var MIN = min*60;
Logger.log(MIN);
var ex = MIN+sec;
Logger.log(ex);
return ex;
}
This is what I have in the script editor. 这就是我在脚本编辑器中所拥有的。 The input is the parameter from the spreadsheet when I write the formula in the sheet itself (ex - =MTOS(3:23.53)).
当我在工作表本身中编写公式时,输入是来自电子表格的参数(例如-= MTOS(3:23.53))。 When I run the function in the script editor, it gives me the error "TypeError: Cannot call method "split" of undefined. (line 5, file "MTOS")" and in sheets, it returns "Error : Result was not a number."
当我在脚本编辑器中运行该函数时,它给我错误“ TypeError:无法调用未定义的方法“ split”。(第5行,文件“ MTOS”)),并且在工作表中,它返回“ Error:结果不是数。” I understand that this is happening because input is not defined in the function itself, so .split cannot work.
我知道发生这种情况是因为在函数本身中未定义输入,因此.split无法正常工作。 But how else can I write the custom formula for sheets?
但是我还能如何为工作表编写自定义公式?
Thank you. 谢谢。
This seems to work for me: (Perhaps I'm misunderstanding the question). 这似乎对我有用:(也许我误解了这个问题)。
function MTOS(input){
var iA = input.split(":");
var min = Number(iA[0]);
var sec = Number(iA[1]);
Logger.log('Seconds=%s',min * 60 + sec);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.