2013-09-05 53 views
0

我正在嘗試使用Google應用程序腳本從表單中獲取信息,但我認爲檢查並查看錶單是否有目的地是個好主意電子表格附加到它。如果是這樣,我想抓住ID,如果它不,我想創建一個。每次我運行下面的代碼時de-bugger會拋出一個錯誤,告訴我「表單當前沒有響應目標。」檢查Google表單是否有目標電子表格

請幫

var form = FormApp.getActiveForm(); 
    var formName = form.getTitle(); 


    function randomQuestion() { 
     var theFormID = form.getId(); 

     var sheetID = function sheetID() { 

     if (form.getDestinationId() === 'undefined') { 
      var ss = SpreadsheetApp.create(formName + ' (Responses)'); 
      form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); 
      return form.getDestinationId(); 

     }else { 
     return form.getDestinationId();} 

     } 
    var sheet = sheetID(); 
    g; // <-un-comment this to make the de-bugger throw an error =) 
    } 

回答

1

的方法getDestinationId()拋出異常時的形式沒有任何響應目標。你需要用try...catch來處理這個問題,是這樣的:

... 
var form = FormApp.getActiveForm(), idResponse; 
try { 
    idResponse = form.getDestinationId(); 
} 
catch(e) { 
    // TODO 
} 
... 
+0

這應該被報告爲一個錯誤! – Mogsdad

+0

至少,文檔應該更具體,並指出該方法可以拋出異常。這以其他方法完成,例如, [openById(id)](https://developers.google.com/apps-script/reference/forms/form-app?hl=en#openById(String))和[openByUrl(url)](https:// developers.google.com/apps-script/reference/forms/form-app?hl=en#openByUrl(String)) – wchiquito

+0

謝謝你的工作......我學到了一些東西!這是我用來測試是否存在電子表格的代碼,如果沒有創建。代碼' var form = FormApp.getActiveForm(); var ssID; try { ssID = form.getDestinationId(); } catch(e){ var exception = e.name; if(exception ===「Exception」){var _ = SpreadsheetApp.create(formName +'(Responses)'); (FormApp.DestinationType.SPREADSHEET,ss.getId()); ssID = form.getDestinationId(); } else { ssID = form.getDestinationId(); } }'code' – user2262933

2

這裏是我用來測試是否有回覆電子表格,如果沒有建立一個準則。

var form = FormApp.getActiveForm(); 
    var formName = form.getTitle(); 
    var ssID; 

    try { 
     ssID = form.getDestinationId(); 
    } 
    catch(e) { 
     var exception = e.name; 
     if (exception === "Exception"){ 
     var ss = SpreadsheetApp.create(formName + ' (Responses)'); 
     form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); 
     ssID = form.getDestinationId(); 
     }else{ 
     ssID = form.getDestinationId(); 
     } 
    } 
相關問題