title: How to get ID value of Google Drive file for input to Google Apps Script
body: <p>I am writing a Google Apps script to send a file from Google Drive to a list of email addresses saved in a Google Spreadsheet. </p>
  
  <p>Since I will be sending a different file every time I use the script, I have my script set up to take the file ID as text input from the user. The only way I've seen to get the ID directly from Drive is to right-click on the file, select "Get Link", copy it to the clipboard, paste it into my form and erase the bits that aren't the ID. I'm writing to ask if anyone knows a better way. I'm also open to comments suggesting a better program design. </p>
  
  <pre class="lang-js prettyprint-override"><code>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.");
      }
    }
  }
  </code></pre>