如何获取Google云端硬盘文件的ID值以输入到Google Apps脚本

时间:2015-11-25 17:14:41

标签: google-apps-script google-drive-api

我正在撰写Google Apps脚本,以便将Google云端硬盘中的文件发送到Google电子表格中保存的电子邮件地址列表。

由于每次使用脚本时我都会发送不同的文件,因此我将脚本设置为将文件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.");
    }
  }
}

2 个答案:

答案 0 :(得分:0)

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

基本上,我在javascript中解析表单输入以仅选择文件夹ID。使用此代码,您实际上可以传入"共享ID" (通过右键单击和"获取链接"方法检索),文件夹URL(当您在Google云端硬盘中的文件夹内时由浏览器地址栏检索)或仅文件夹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;

答案 1 :(得分:0)

这不是我的,我们已经在这个论坛上通过了很多。

此方法之所以有效,是因为无论用户粘贴id还是url,它都能得到您想要的:

function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }

因此您无需对自己删除其余的URL保持谨慎。