简体   繁体   English

Google App脚本-如何在不定义对象的情况下使用方法

[英]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.

相关问题 如何在Google App脚本中返回对象 - How to return object in google app script 如何使用Pagetoken Google App脚本 - how to use pagetoken google app script 如何在不使用Google SDK的情况下通过键脚本在angularjs中使用Google Map - how to use google map in angularjs with script of key Without Google SDK 如何在谷歌网站中使用谷歌脚本 web 应用程序和多个页面? - How to use google script web app with multiple pages in google site? 如何刷新标签而不重新加载Google App脚本中的页面 - How to refresh tag without reloading page in google app script 如何在Google应用程序脚本中运行触发器而无需每次都重新认证 - How to run the triggers in Google app script without reauth each time 如何在内容脚本中使用Google +1而不会出现内容安全政策错误? - How to use Google +1 in a content script without Content Security Policy error? 如何在 function 调用谷歌应用脚本中的按钮时传递 object - How to pass an object inside a function call of a button in google app script 如何为searchFolders()函数正确使用Google App Script“非”查询运算符 - How to properly use Google App Script “not” query operator for searchFolders() function 如何在 html 小程序中使用谷歌应用脚​​本属性服务 - How to use google app script property service in a html applet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM