Google Apps脚本:检查上传文件是否存在或为空 [英] Google Apps Script: Check upload file exist or empty

查看:103
本文介绍了Google Apps脚本:检查上传文件是否存在或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立条件来检查文件是否在提交之前上传。目前的情况是,发送/提交上传的文件是否成功。当我尝试使用 var file = e.parameter.file 时,它会给出一个字符串 FileUpload 。任何人都可以帮助我吗?

I try to build condition to check either the file is uploaded before submit. Current situation is, it success to send/submit either file uploaded or not. When I try to get the file use var file = e.parameter.file, it give a string FileUpload. Can anyone help me?

function  setFormUpload() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var app = UiApp.createApplication().setTitle("Upload File");
  var form = app.createFormPanel().setId('form').setEncoding('multipart/form-data');
  var formContent = app.createVerticalPanel();
  var infoBox = app.createLabel().setVisible(false).setId('infoBox');

  form.add(formContent);  
  formContent.add(app.createFileUpload().setName('file'));
  formContent.add(app.createSubmitButton('Upload'));
  app.add(form);
  sheet.show(app);
}


function doPost(e){
  var app = UiApp.getActiveApplication();
  var file = e.parameter.file;

  if(file == ''){
    var text = 'file empty';
  } else {
    var text = 'file exist';
  }

  app.getElementById('infoBox').setText(text).setVisible(true);
  return app;
}


推荐答案

的e.parameter.file,(因为这总是返回FileUpload是否为空)检查它的长度。如果没有文件,它将为零。

Instead of checking the actual value of e.parameter.file, (as this always return FileUpload whether empty or not) check its length. It will be zero if there is no file.

if(file.length > 0){
    var text = 'file empty';
} else {
    var text = 'file exist';
}

这篇关于Google Apps脚本:检查上传文件是否存在或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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