Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 上载文件重命名后保留文件扩展名-应用程序脚本_Google Apps Script - Fatal编程技术网

Google apps script 上载文件重命名后保留文件扩展名-应用程序脚本

Google apps script 上载文件重命名后保留文件扩展名-应用程序脚本,google-apps-script,Google Apps Script,因此,我有一个HTMLservice将一个文件blob上传到一个google驱动器文件夹,该文件夹也正在重命名该文件。我遇到的问题是重命名操作实际上导致文件扩展名被删除。我最初试图做的是添加一个函数来检索文件扩展名,然后在.rename操作中添加它,但是如果有更有效的方法保留扩展名,我将使用它 代码.gs var submissionSSKey = 'SSId'; var folderId = "FolderId"; function doGet(e) { var template = H

因此,我有一个HTMLservice将一个文件blob上传到一个google驱动器文件夹,该文件夹也正在重命名该文件。我遇到的问题是重命名操作实际上导致文件扩展名被删除。我最初试图做的是添加一个函数来检索文件扩展名,然后在
.rename
操作中添加它,但是如果有更有效的方法保留扩展名,我将使用它

代码.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>
<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>

//“提交”按钮处理程序调用的Javascript函数,
//显示结果。
函数updateOutput(resultHtml){
切换_可见性(“inProgress”);
var outputDiv=document.getElementById('output');
outputDiv.innerHTML=resultHtml;
}
//来自blog.movalog.com/a/javascript-toggle-visibility/
功能切换\u可见性(id){
var e=document.getElementById(id);
如果(e.style.display=='block')
e、 style.display='none';
其他的
e、 style.display='block';
}
姓名:
部门:
选择选项
出纳员
迎宾员
跑步者
线路控制
泛美开发银行
不为人知
电邮:
信息:
学校时间表(仅图像文件):
上传。请稍候。。。
谢谢。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>
<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月23日晚上11:59之前提交

您将从表单中获得一个名称,因此您可以在Google Drive上创建文件之前设置名称。中展示了在各种场景中获取扩展的一种很好的技术,因此我借用了这一技术:

  ...
  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);
  ...