2017-04-26 84 views
0

有沒有一種快速的方式來以編程方式將所有回覆從Google表單導出到csv?類似於通過腳本調用的「導出對csv的響應」。將表單回覆導出爲csv Google Apps腳本

現在我做一個巖畫方式:

  • 遍歷我要導出的形式(〜75)
    • 打開每個窗體var form = FormApp.openById(formId);
    • 獲取回覆:var formReponses = form.getResponses();(每個表格有0到700個回覆)
    • 迭代回覆並獲得項目響應:var preguntes = formReponses[r].getItemResponses();
      • 對於每個itemResponse,將其轉換爲CSV/JSON
    • 出口響應驅動文件

這是的極端緩慢,另外它掛了個遍 ,所以我必須以50個響應塊的形式導出響應,並將它們保存在Drive分隔的文件中。在下一次執行時(讓服務器冷卻一段時間後),我再次執行腳本,跳過在塊文件中發現的響應數。

另外我不知道,谷歌保持的響應命令時做form.getResponses();(其實我發現,如果表單已被修改,順序是不一樣的)

有沒有更好的辦法做它?

+0

我假設你的答覆保存到電子表格中,你可能會加快這[的GetValues](https://developers.google.com/apps-script/reference/spreadsheet/range#getValues())。然後創建一個CSV文件,如[SO帖子]中所述(http://stackoverflow.com/questions/28327109/google-apps-script-convert-to-csv) –

+0

不可以,這些回覆不是以電子表格的速度加快數據介紹。基本上,在該過濾器後面有一個腳本應用程序,並將不同的表單數據關聯起來並將其呈現給用戶 – Miquel

+1

您可以使用以下鏈接下載表單回覆:https://docs.google.com/forms/d/{form ID此處}/downloadresponses?TZ_OFFSET = -18000000'。通過修改表單ID,這在瀏覽器中運行良好,但我無法使用它與UrlFetchApp一起工作(授權失敗),也許您可​​以指出這一部分。 –

回答

0

在@JackBrown的幫助下,我設法編寫了一個Chrome擴展來下載響應(可能很快會在github中)。這將等待在formIds對象每次下載直到完成,然後提示下一個:通過使用從片得到的值

'use strict'; 

function startDownload() { 
    const formIds = { 
     'Downloads-subfolder-here': { 
      'Download-filename-here': '1-cx-aSAMrTK0IHsQkE... {form-id here}', 
      'Another-filename-here': '...-dnqdpnEso {form-id here}', 
      // ... 
     }, 
     'Another-subfolder-here': { 
      'Download-filename-here': '1-cx-aSAMrTK0IHsQkE... {form-id here}', 
      'Another-filename-here': '...-dnqdpnEso {form-id here}', 
      // ... 
     }, 
    }; 

    const destFolders = Object.keys(formIds); 
    const downloads = []; 

    for (let t = 0, tl = destFolders.length; t < tl; t += 1) { 
     const destFolder = destFolders[t]; 
     const forms = Object.keys(formIds[destFolder]); 

     for (let f = 0, fl = forms.length; f < fl; f += 1) { 
      const formName = forms[f]; 
      downloads.push({ 
       destFolder, 
       formName, 
       url: `https://docs.google.com/forms/d/${formIds[destFolder][formName]}/downloadresponses?tz_offset=-18000000`, 
       filename: `myfolder/${destFolder}/${formName.replace(/\//g, '_')}.csv`, 
      }); 
     } 
    } 

    const event = new Event('finishedDownload'); 
    const eventInterrupt = new Event('interruptedDownload'); 
    let currId; 

    chrome.downloads.onChanged.addListener((downloadDelta) => { 
     if (downloadDelta.id === currId) { 
      if (downloadDelta.state && downloadDelta.state.current === 'complete') { 
       document.dispatchEvent(event); 
      } else if (downloadDelta.state && downloadDelta.state.current === 'interrupted') { 
       console.log(downloadDelta); 
       document.dispatchEvent(eventInterrupt); 
      } 
     } 
    }); 

    downloads.reduce((promise, actual) => { 
     return promise.then((last) => (last ? new Promise((resolve) => { 
      const { url, filename, destFolder, formName } = actual; 
      function listener() { 
       document.removeEventListener('finishedDownload', listener); 
       document.removeEventListener('interruptedDownload', listener); 
       resolve(true); 
      }; 
      function interrupt() { 
       document.removeEventListener('finishedDownload', listener); 
       document.removeEventListener('interruptedDownload', listener); 
       resolve(false); 
      } 
      console.log(`Processant ${destFolder}, ${formName}: ${url}`); 
      document.addEventListener('finishedDownload', listener); 
      document.addEventListener('interruptedDownload', interrupt); 
      chrome.downloads.download({ url, filename }, (downloadId) => { 
       currId = downloadId; 
       if (!downloadId) { 
        console.log(); 
        console.log('Error downloading...'); 
        console.log(runtime.lastError); 
        resolve(); 
       } 
      }); 
     }) : Promise.resolve(false))); 
    }, Promise.resolve(true)); 
} 

chrome.browserAction.onClicked.addListener((/*tab*/) => startDownload());