重命名上载文件后保留文件扩展名 - App Script

时间:2013-12-28 23:23:17

标签: google-apps-script

所以我有一个HTML服务上传文件blob到google驱动器文件夹,该文件夹也重命名该文件。我遇到的问题是重命名操作实际上导致文件扩展名被删除。我最初尝试做的是添加一个函数来检索文件扩展名,然后在.rename操作中添加它,但如果有更有效的方法来保留扩展名,我会接受它。

Code.gs

var submissionSSKey = 'SSId';
var folderId = "FolderId";

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('form.html');
  template.action = ScriptApp.getService().getUrl();
  return template.evaluate();
}

function processForm(theForm) {


  // Fill in response template
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  var comp = 'N/A'
  var name = template.name = theForm.name;
  var department = template.department = theForm.department;
  var message = template.message = theForm.message;
  var email = template.email = theForm.email;

  var fileBlob = theForm.myFile;
  var folder = DocsList.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);
  //var fileUrl = template.fileUrl = doc.getUrl();
  var rename = doc.rename(department+' - '+name);     

    var now     = new Date(); 
    var year    = now.getFullYear();
    var month   = now.getMonth()+1; 
    var day     = now.getDate();
    var hour    = ((now.getHours()>12)?(now.getHours()-12):now.getHours());;
    var minute  = now.getMinutes();
    var second  = now.getSeconds(); 
    if(month.toString().length == 1) {
        var month = '0'+month;
    }
    if(day.toString().length == 1) {
        var day = '0'+day;
    }   
    if(hour.toString().length == 1) {
        var hour = '0'+hour;
    }
    if(minute.toString().length == 1) {
        var minute = '0'+minute;
    }
    if(second.toString().length == 1) {
        var second = '0'+second;
    }   
    var dateTime = month+'/'+day+'/'+year+' '+hour+':'+minute+':'+second;

  // Record submission in spreadsheet
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 6).setValues([[comp,name,department,message,email,dateTime]]);


  // Return HTML text for display in page.
  return template.evaluate().getContent();
}

form.html

<script>
  // Javascript function called by "submit" button handler,
  // to show results.
  function updateOutput(resultHtml) {
    toggle_visibility('inProgress');
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = resultHtml;
  }

  // From blog.movalog.com/a/javascript-toggle-visibility/
  function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
      e.style.display = 'none';
    else
      e.style.display = 'block';
  }
</script>

<div id="formDiv" style="display: block;">
<!-- Form div will be hidden after form submission -->
<form id="myForm">
<table>
    <tr><td>Name:</td> <td><input name="name" type="text" /></td>
    <tr><td>Department:  <td><select name="department">
    <option>Select Option</option>
    <option>Cashier</option>
    <option>Greeter</option>
    <option>Runner</option>
    <option>Line Control</option>
    <option>IDB</option>
    <option>Unknown</option>
    </select></td>
    <tr><td>Email:</td> <td><input name="email" type="text" /></td>
    <tr><td>Message:</td> <td><textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea></td>
    </table>

    School Schedule (Image Files Only): <input name="myFile" type="file"/><br/>
  <input type="button" value="Submit" id="submit"
      onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
</form>
</div>

<div id="inProgress" style="display: none;">
<!-- Progress starts hidden, but will be shown after form submission. -->
<img src="https://dl.dropboxusercontent.com/u/211279/loading3T.gif" alt="Loading">Uploading. Please wait...
</div>

<div id="output">
  <!-- Blank div will be filled with "Thanks.html" after form submission. -->
</div>

Thanks.html

<div>
    <h1>Thanks</h1>
    <p>Thank you for your submission.</p>
    Name: <?= name ?><br/>
    Department: <?= department ?><br/>
    Message: <?= message ?><br/>
    Email: <?= email ?><br/>

    <p><b>Note:&nbsp;</b>All requests for work schedule changes must be submitted by 11:59PM January 23rd.</p>

</div>

1 个答案:

答案 0 :(得分:1)

您从表单中获得了Blob,因此您可以在Google云端硬盘上创建文件之前设置名称。在How to extract extension from filename string in Javascript?中显示了一种在各种场景中抓取扩展的好方法,所以我只是借用了它:

  ...
  var fileBlob = theForm.myFile;
  var fname = fileBlob.getName();
  var newName = "new_file_name";  // simple example
  // extract extension using RegEx
  // from https://stackoverflow.com/a/680982/1677912
  var extensionfinder = /(?:\.([^.]+))?$/; 
  var ext = extensionfinder(fname)[1];
  fileBlob.setName(newName+'.'+ext);
  var folder = DriveApp.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);
  ...