2015-10-19 14 views
0

我正在嘗試使用JavaScript indexof搜索urlfetchapp響應中的字符串,但出現錯誤!你能告訴我我在這裏做錯了什麼,以及如何查找URL的源代碼中的字符串?如何搜索UrlFetchApp.fetch響應中的字符串?

**這是錯誤:**類型錯誤:無法找到函數的indexOf在對象

var url = "http://www.bbc.com"; 
var options = 
    { 
    headers : {'Cache-Control' : 'max-age=0'} 
    }; 
var response = UrlFetchApp.fetch(url, options) 


if(response.indexOf("sun shine")>-1) 
    { 
    var myVar="string found"; 
    SpreadsheetApp.getActiveSheet().getRange('a1').setValue(myVar); 
    } 

回答

0

UrlFetchApp.fetch返回HTTPResponse object它不具有的indexOf方法。 如果您嘗試解析網頁的內容,您可以嘗試以下方法,該方法返回一個字符串並可以進行搜索。

var response = UrlFetchApp.fetch(url, options).getContentText();