2017-10-04 35 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

相關問題