2017-10-04 37 views
0

所以我有一个跟踪里程的Google表单,它通过运行Yogi's IMPORTXML function here来计算。问题是......它超时。很多。它可能会在25%的时间内抛出#N/A。我使用copyDown加载项将其应用于每个表单条目,因此最简单的解决方案就是使用原始公式转到单元格,将其复制并粘贴到犯错误的单元格中(这似乎也提高了我的机会首先清除单元格内容)Google表格脚本修复可预测的错误

我已经为此工作表设置了一个自定义菜单,并且想要添加一个脚本来修复错误。我只是无法弄清楚开始的方向(我对脚本不太了解,更多的是“从互联网上复制一些东西并将其调整为我的需求”类型)。最终的目标是让它从第3行搜索一个特定的列,找出其中包含#N/A的单元格,清除它们,从R2粘贴公式,然后将它们加载到结果中,然后仅复制/粘贴值所以函数不必重新计算。我想如果仍然有错误,他们可以再次做...我宁愿没有它运行一个无限循环(虽然我可能偶尔也坚持它的时间触发器)

谢谢!

回答

0

我会通过消除对IMPORTXML功能的需求来解决这个问题。

您提到您已经在使用Google Apps脚本编写脚本,因此下一步是实施一个脚本来完成IMPORTXML功能的功能 - 计算2个点之间的距离。

您可以使用onFormSubmit trigger执行选择的功能,并通过传递参数(即function runsWhenFormSubmits(event))并因此使您的功能能够访问表单提交object。你可以使用built-in Maps API。您链接的电子邮件线程具有实际distance code already

function getDirections(start, finish) { 
    // Start & finish are viable text addresses 
    var df = Maps.newDirectionFinder(); 
    df.setOrigin(start).setDestination(finish); 
    return df.getDirections(); 
} 

的对象布局显示here (in JSON format).

总结:

function runsWithForm(e) { 
    var origin = "123 Fake Street, Springfield, IL", 
     dest = "321 Real Street, Springfield, MO"; 
    // When called via the Script Editor, the event object is not present. 
    if(e) { 
    var formItems = e.response.getItemResponses(); 
    var startAddressIndex = 0; // If the starting address is the first answer. 
    var destinationAddressIndex = 1; // If the destination address is the 2nd answer. 
    // Assuming the response is a simple String (vs. String[], etc). 
    origin = formItems[startAddressIndex].getResponse(); 
    dest = formItems[destinationAddressIndex].getResponse(); 
    } 
    var directions = getDirections(origin, dest); 
    if(directions.routes.length == 0) 
    throw new Error("No routes found between '" + origin + "' and '" + dest + "'."); 

    // Add code to do stuff with the existing route. 
} 
+0

虽然这是一个可能的选项,以克服我的烦恼......的IMPORTXML函数实际上是这个脚本的替代品,就像这张表格中的原始解决方案。它远远不可靠,所以当我发现IMPORTXML选件及其更高的可靠性时,我感到非常兴奋。无论哪种方式,对Google地图的呼叫有时似乎只是超时(或某些),我正在寻找一种方法来自动或简单地(一个菜单选项)为用户(而不是我)来清除它们!我以其他方式取得进展,部分要归功于你对我另一个问题的回复。谢谢! – Alex

相关问题