2015-11-25 140 views
0

我正在編寫一個Google Apps腳本,用於將Google Drive中的文件發送到保存在Google Spreadsheet中的電子郵件地址列表。如何獲取Google Drive文件的ID值以便輸入到Google Apps腳本

由於每次使用腳本時我都會發送一個不同的文件,因此我設置了我的腳本以將文件ID作爲用戶的文本輸入。我看到直接從雲端硬盤獲取ID的唯一方法是右鍵單擊該文件,選擇「獲取鏈接」,將其複製到剪貼板,粘貼到我的表單中並擦除不是ID的位。我在寫信詢問有沒有人知道更好的方法。我也樂於提出建議更好的程序設計的意見。

function sendEmails() { 
    var id = "gibberish"; //email spreadsheet 
    SpreadsheetApp.openById(id); 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    Logger.log(sheet.getName()); 
    var startRow = 2; // First row of data to process 
    var numRows = 2; // Number of rows to process 
    // Fetch the range of cells A2:B3 
    var dataRange = sheet.getRange(startRow, 1, numRows, 2); 
    // Fetch values for each row in the Range. 
    var data = dataRange.getValues(); 

    //Get subject line from user 
    var ui = SpreadsheetApp.getUi(); 
    var response = ui.prompt('Enter subject: ', ui.ButtonSet.OK_CANCEL); 

    var subject; 
    // Process the user's response. TODO- error checking 
    if (response.getSelectedButton() == ui.Button.OK) { 
    subject = response.getResponseText(); 
    } else { 
    Logger.log('The user either canceled or clicked the close button in the dialog\'s title bar.'); 
    subject = "No subject"; 
    } 

    //get id for attachment file 
    var ui2 = SpreadsheetApp.getUi(); 
    var response2 = ui2.prompt('Enter Drive id for attachment: ', ui.ButtonSet.OK_CANCEL); //TODO- error checking 

    var attachmentID; 
    var file = null; 

    if (response2.getSelectedButton() == ui.Button.OK) { 
    attachmentID = response2.getResponseText(); 
    file = DriveApp.getFileById(attachmentID); 
    Logger.log('The user entered %s', response2.getResponseText()); 
    } else { 
    Logger.log('The user either canceled or clicked the close button in the dialog\'s title bar.'); 
    } 

    for (i in data) { 
    var row = data[i]; 
    var emailAddress = row[0]; // First column 
    var message = "Time Sheet attached. \n\n -Jessica";  
    if (file != null) { //TODO- or if file is right file 
     MailApp.sendEmail(emailAddress, subject, message, {attachments: [file]}); 
    } else { 
     Logger.log("No file was attached. Email not sent."); 
    } 
    } 
} 
+0

http://stackoverflow.com/questions/25180042/load-file-from-google-drive-into-a-form-using-apps-script看起來像一個更好的設計。我會玩這個。還會欣賞其他答案/評論 – jgloves

回答

0

我在Copy Folder script中做了類似的事情。

基本上,我解析了JavaScript中的表單輸入以僅選擇文件夾ID。使用此代碼,您實際上可以傳入「共享ID」(通過右鍵單擊和「獲取鏈接」方法獲取),文件夾URL(當您位於Google Drive中的文件夾內時,由瀏覽器地址欄檢索),或者只是文件夾ID。 JavaScript解析輸入並用替換表單條目,只是文件夾ID,以便您的Google腳本可以正常檢索此表單數據。

您可以查看source code for the project,但這裏是相關部分。在我的網絡應用程序中,它位於JavaScript.html文件中。

// Regular expression - find string beginning with "id=" 
// http://www.w3schools.com/jsref/jsref_regexp_source.asp 
var regex = /id=/; 
// NOTE: pretty sure you could just make a string variable = "id=" instead of regex, but I didn't want to mess up my script 

// Set a temporary variable to the value passed into the "folderId" field 
var fId = thisForm.folderId.value; 

// Get the index of the string at which the folderId starts 
var idStart = fId.search(regex); 
var foldersStart = fId.search("folders"); 
if (idStart > 0) { 
// Slice the string starting 3 indices after "id=", which means that it takes away "id=" and leaves the rest 
fId = fId.slice(idStart+3); 
} else if (foldersStart > 0) { 
fId = fId.slice(foldersStart + 8); 
} 

// Find the ampersand in the remaining string, which is the delimiter between the folderId and the sharing privileges 
var amp = fId.indexOf("&"); 

// Slice the string up to the ampersand 
if (amp > 0) { 
fId = fId.slice(0,amp); 
} 

// Set the folderId element within thisForm (retrieved from doGet) to the new, sliced fId variable 
thisForm.folderId.value = fId; 
相關問題