2015-05-06 75 views
1

這工作得很好:UrlFetchApp愁楚

var resp = UrlFetchApp.fetch("someremotehost/SomeFile.csv"); 
resp.getResponseCode(); //returns 200 
resp.getContentText(); //returns the data 

然而,我的本地機器上,我運行XAMPP與SomeFile.csv位於htdocs目錄的/ dev但我不能讓它在localhost工作:

var resp = UrlFetchApp.fetch("localhost/dev/SomeFile.csv"); 
resp.getResponseCode(); //returns 0.0 
resp.getContentText() //returns nothing! 

我用鍍鉻擴展郵遞員檢查和http://localhost/dev/SomeFile.csv工作正常,所以爲什麼UrlFetchApp.fetch(「http://localhost/dev/SomeFile.csv」)不起作用?

+0

嘗試在另一臺機器上運行xampp,然後使用ipaddress訪問並查看它是否有效。 – KRR

+1

與此主題有關的以前的問題:[here](http://stackoverflow.com/questions/17440179/unable-to-connect-google-apps-script-to-mysql-through-localhost),[here](http ://stackoverflow.com/questions/13151909/google-apps-script-jdbc-connection-failed)和[here](http://stackoverflow.com/questions/24659033/connection-to-sql-server-2008 -R2-DB-從-A-谷歌擴張)。我確信我已經看到更多,但是現在找不到它們。這是一個常見的誤解,GAS代碼正在「在雲端」運行,並且無法訪問您的計算機。 – Mogsdad

回答

2

這不會工作,因爲應用腳本執行代碼服務器端(在谷歌服務器)。做到這一點的唯一方法是製作一個htmlService應用程序並使用前端的ajax。

+0

齊格,非常感謝您的建議!我仍然對應用程序腳本很熟悉,UrlFetchApp文檔似乎很遺憾。 :-( – Rx2000

+0

)如果您將本地主機連接到互聯網上(例如在路由器上使用DMZ或端口映射),那麼您可以使用其*互聯網地址*和端口從Apps Script訪問它,但「localhost 「永遠不會自動解決你的機器。 – Mogsdad