如何重命名上载到应用程序脚本Web应用程序表单的文件? [英] How do I rename files uploaded to an apps script web app form?

查看:62
本文介绍了如何重命名上载到应用程序脚本Web应用程序表单的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将发现的一些代码拼接在一起制作了一个小的Web应用程序表单.它对我来说几乎是完美的,允许我将文件上传到Google云端硬盘文件夹,将表单中提交的数据记录在电子表格中,并在文件上传时通过电子邮件发送给我.

I've made a little web app form by splicing together some code I found. It works nearly perfectly for me, allowing me to upload files to a Google Drive folder, logging the data submitted in the form in a spreadsheet and emailing me when a file is uploaded.

但是,我真正想做的是根据表单数据重命名上载的文件.例如,如果输入的制造商值="Sony",日期值= 12-04-2016,则将文件名设置为"Sony_12-04-2016.pdf"

However, what I really want to be able to do is to rename the files that are uploaded according to the form data. For example, if the inputted manufacturer value = "Sony" and the date value = 12-04-2016, then make the filename "Sony_12-04-2016.pdf"

通过尽可能地查找,似乎我需要将提交的值传递到createFile()函数中,但是我对编码还很陌生,不确定自己在做什么..

From looking it up as best I can it seems I need to pass the submitted values into the createFile() function but I'm quite new to coding and not really sure what I'm doing here..

这是我到目前为止所拥有的:

Here's what I have so far:

.gs

var TO_ADDRESS = "my email address";

function doGet(e) {
    return HtmlService.createTemplateFromFile('index')
    .evaluate()
    .setTitle('Price List Upload Form')
}

function processForm(theForm) {
  var fileBlob = theForm.fileUpload;

  Logger.log("fileBlob Name: " + fileBlob.getName())
  Logger.log("fileBlob type: " + fileBlob.getContentType())
  Logger.log('fileBlob: ' + fileBlob);

  var fldrSssn = DriveApp.getFolderById('my Google Drive folder id');
    fldrSssn.createFile(fileBlob);

  return true;
}

function formatMailBody(obj) {
  var result = "";
  for (var key in obj) {
    result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + obj[key] + "</div>";
  }
  return result;
}

function doPost(e) {

  try {
    Logger.log(e);
    record_data(e);

    var mailData = e.parameters;

    MailApp.sendEmail({
      to: TO_ADDRESS,
      subject: "New Price List Uploaded",
      htmlBody: formatMailBody(mailData)
   });

    return ContentService
          .createTextOutput(
            JSON.stringify({"result":"success",
                            "data": JSON.stringify(e.parameters) }))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(error) {
    Logger.log(error);
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  }
}

function record_data(e) {
  Logger.log(JSON.stringify(e));
  try {
    var doc     = SpreadsheetApp.getActiveSpreadsheet();
    var sheet   = doc.getSheetByName('responses');
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1;
    var row     = [ new Date() ];
    for (var i = 1; i < headers.length; i++) {
      if(headers[i].length > 0) {
        row.push(e.parameter[headers[i]]);
      }
    }
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
  }
  catch(error) {
    Logger.log(e);
  }
  finally {
    return;
  }

}

.html

<form id="gform" autocomplete="on" method="POST" class="pure-form pure-form-stacked"
action="script url" onsubmit="picUploadJs(this)">

<fieldset class="pure-group">
  <input name="fileUpload" type="file" />
</fieldset>

<fieldset class="pure-group">
  <label for="manufacturer">Manufacturer: </label>
  <input id="manufacturer" name="manufacturer" type="text" placeholder="Manufacturer Name" required/>
</fieldset>

<fieldset class="pure-group">
  <label for="issueDate">Date Issued: </label>
  <input id="issueDate" name="issueDate" type="date" required />
</fieldset>

<fieldset class="pure-group">
  <label for="info">Additional Info: </label>
  <input id="info" name="info" type="text" placeholder="Any Additional Information"/>
</fieldset>

<fieldset class="pure-group">
  <input id="email" name="email" type="hidden" value="test@gmail.com"/>
</fieldset>

<button class="button-success pure-button button-xlarge">
  Upload</button>

  <div style="display:none;" id="thankyou_message">
  <div id="status" style="display: none">
  <h2>Uploading. Please wait...</h2>
    </div>
  </div>

function picUploadJs(frmData) {

document.getElementById('status').style.display = 'inline';

  google.script.run
.withSuccessHandler(updateOutput)
.processForm(frmData)
};

function updateOutput() {

var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "<h2>File successfully uploaded!</h2><button class=\"pure-button\"><a href=\"page url" style=\"text-decoration: none;\">Upload another</a></button>";
  }

原始代码来自此处

此处

推荐答案

我可能不是所有正确的术语,但是您必须将表单提交字段转换为变量,才能在.gs脚本中使用它们.将它们转换为变量后,您可以根据自己的喜好构建"文件名,并在将fileBlob写入文件时使用它.鉴于上面的代码,您应该能够只修改processForm函数,如下所示:

I probably don't have all the lingo correct, but you have to turn the form submission fields into variables to be able to use them in your .gs script. Once you turn them into variables, you can "build" a filename to your liking, and use it when writing the fileBlob to a file. Given your code above, you should be able to just modify the processForm function as follows:

function processForm(theForm) {
  var fileBlob = theForm.fileUpload;
  var manufacturer = theForm.manufacturer;
  var issueDate = theForm.issueDate;
  var myNewFilename = manufacturer + '_' + issueDate + '.pdf';
  fileBlob.setName(myNewFilename);  //set Name of the blob
  var fldrSssn = DriveApp.getFolderById('my Google Drive folder id');
  fldrSssn.createFile(fileBlob);  // create a file with the blob, name of the file will be the same as set by setName method
  return true;
}

让我也注意一些可能对将来的访问者有用的方法-如何将时间戳记写入文件名.使用Utilities.formatDate函数设置一个新变量,然后可以将该变量连接到一个文件名中,如上例所示.设置变量的方法如下:

Let me also note something that may be helpful for future visitors--how to write a timestamp into the filename. Set a new variable using the Utilities.formatDate function, and then you can concatenate this variable into a filename like in the example above. Here's how to set the variable:

var myTS = Utilities.formatDate (new Date(), Session.getScriptTimeZone(), "yyyyMMdd_HHmmss--") ;

格式是完全灵活的-只需查找功能以获取详细信息即可.

Format is completely flexible--just look up the function for details.

这篇关于如何重命名上载到应用程序脚本Web应用程序表单的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆