2013-09-21 80 views
0

我試圖從Drive中文件夾中的每個Google文檔中提取文本,並將文本粘貼到Google電子表格的第一列,以便文件1的內容位於A1 ,A2中的文件2的內容等等。最終,我試圖重新創建存儲在所有這些文件中的信息的數據庫,因此如果文本可以按字段分割得越多越好,但我認爲這在Excel中應該是微不足道的使用文本到列。將幾個Google文檔中的文本提取到Google電子表格中

我已經使用了一些片段在線刺傷它,但我現在難倒了。

這裏是我的腳本,因爲它代表:

//Function to extract the body from each document in a folder and copy it to a spreadsheet 
function extract() { 

//Define the folder we're working with ("Communication Passports") and get the file list 
var folder = DocsList.getFolder("Communication Passports"); 
var contents = folder.getFiles(); 

//Define the destination spreadsheet file (CP) and set up the sheet to receive the data 

var ss = SpreadsheetApp.openById("0AicdFGdf-Cx5dHFTX1R3Wm1RTEFTZ2d5ZmxuSjJSOHc"); 
SpreadsheetApp.setActiveSpreadsheet(ss); 
Logger.log('File name: ' + ss.getName()); 
var sheet = SpreadsheetApp.getActiveSheet(); 
sheet.clear(); 
sheet.appendRow(["Name", "Date", "Contents", "URL", "Download", "Description"]); 

//Set up other variables 
var file; 
var data; 

//Loop through and collect the data (I don't actually need this - just borrowed the code from a snippet online - but it is SO CLOSE!) 
//Sadly, getBody doesn't work on files, only on documents 
for (var i = 0; i < contents.length; i++) { 
file = contents[i]; 

data = [ 
    file.getName(), 
    file.getDateCreated(), 
    file.getViewers(), 
    file.getUrl(), 
    "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(), 
    file.getDescription() 
]; 

sheet.appendRow(data); 


//Extract the text from the file (this doesn't work at present, but is what I actually need) 


var doc = DocumentApp.openById(file.getId()); 
var body = doc.getBody(); 

//Find a way to paste the extracted body text to the spreadsheet 
} 
}; 

任何幫助將是非常感激地收到 - 我不是一個程序員,我是一個老師和信息是我們學校的孩子的學習需求(有人在夏天刪除了數據庫,我們的備份只能回到一個月!)。

感謝,

西蒙

+0

您能否顯示一個典型的文檔內容,以便我們可以看到需要檢索哪種數據? –

回答

0

嘗試添加:

var doc = DocumentApp.openById(file.getId()); 
body = doc.getBody().getText(); 

返回文檔的實際內容。

我寫了另一個函數來解析內容到更多可用的塊,然後傳遞迴數據表中的一個條目,它工作正常。

相關問題