2012-05-10 61 views
2

我在Google文檔表中使用ImportXML來從sistrix api中獲取數據。它工作正常,但我在一張表中遇到了50個ImportXML命令的限制。所以我使用了一個腳本,它將ImportXML命令寫入單元(臨時)公式,並將單元的結果值取回並將其複製到目標單元。因此,您可以根據需要執行儘可能多的ImportXML查詢,因爲它們只出現在工作表中的一個臨時單元格中。 這裏的問題是,ImportXML查詢SOMETIMES需要很長時間或者返回N/A。Google文檔ImportXML從腳本中調用

有可能我的腳本有時不會等待ImportXML查詢返回,所以結果被破壞?我目前做的是這樣的:

function GetFormulaData(formula, sheet, row, col) 
{ 

// write the formula (ImportXML(...)) to the specified cell 
sheet.getRange(row, col).setFormula(formula); 

// return the value of this cell resulting from the formula 
return sheet.getRange(row, col).getValue(); 
} 

因此,這顯然只能如果公式(該IMPORTXML查詢)完成,並已寫入的返回值進入細胞的工作,這樣我就可以讀出事後。 有沒有人有經驗或替代方案從腳本調用ImportXML?

電賀 Michbeck

我都以不同的方式,現在解決了這個。在Google Doc腳本中使用UrlFetchapp()比ImportXML更常見。但是你必須從http響應中獲得xml數據。這個問題可以關閉。

回答

1

我已經以不同的方式解決了這個問題。在Google Doc腳本中使用UrlFetchapp()比ImportXML更常見。但是你必須從http響應中獲得xml數據。

我這樣做是在現在這樣:

var response = UrlFetchApp.fetch(apiString).getContentText(); 
var xmlContent = Xml.parse(response, true); 
var answer = xmlContent.response.answer; 

// get what you need from the XML answer 
if (answer != null) 
{ 
    var element = answer.getElement('foo'); 
    if (element != null) 
    { 
     var attrib = element.getAttribute('bar');  
     if (attrib != null) 
      value = attrib.getValue(); // the value you want 
    } 
} 
+0

@ Michbeckable.How使用urlfetchapp獲得谷歌搜索結果的標題,描述和URL()?它是否更好,然後importxml?它是否會減少#N/A錯誤?當從google腳本使用importxml並尋找解決方案時,我經常會得到#N/A希望您能幫助我。提前致謝。 – user1788736

+0

@ user1788736:我不知道它如何與谷歌搜索結果一起工作,但應該與查詢Sistrix API類似。我編輯了上面的答案以顯示我使用的一些代碼。 – Michbeckable