上传Google表单中的文件 - 意外错误

时间:2015-04-24 14:46:06

标签: google-apps-script google-sheets google-form

我从链接中复制了此模板并对其进行了编辑。但是,一切都运行正常,直到我单击提交并出现此消息:遇到错误 - 发生意外错误。

我检查了执行记录,这是问题:

 var targetRange = sheet.getRange(lastRow+1, 1, 1, 1).setValues([[Name,Nationality,EmailAddress,DonationAmount]]);

我是脚本新手,所以有人可以查看我的脚本吗?非常感谢。 另外,如果可能的话,我想再向我的脚本添加2个文件上传,但它不起作用。

// Script-as-app template.
var submissionSSKey = '1nqvlVHIi_HQOBRIoyIhoo4u0ok-g4mzssrHeEIIz99g';

function doGet(e) {
var app = UiApp.createApplication().setTitle('HowIMetMyself Registration');
var panel = app.createFormPanel();
var grid = app.createGrid(8,2).setId('registrationGrid');
var nameLabel = app.createLabel('Name');
var nameTextbox = app.createTextBox().setWidth('150px').setName('name');
var nationalityLabel = app.createLabel("Nationality");
var nationalityTextbox = app.createTextBox().setWidth('150px').setName('nationality');
var emailaddressLabel = app.createLabel('Email Address');
var emailaddressTextbox = app.createTextBox().setWidth('150px').setName('email address');
var donationamountLabel = app.createLabel('Donation Amount');
var donationamountTextbox = app.createTextBox().setWidth('150px').setName('donation amount');
var submitButton = app.createSubmitButton('<B>Submit</B>'); 
var warning = app.createHTML('<B>PLEASE WAIT WHILE DATA IS UPLOADING<B>').setStyleAttribute('background','yellow').setVisible(false)
//file upload
var upLoadTypeLabel = app.createLabel('File Upload');
var upLoad = (app.createFileUpload().setName('thefile'));
//file upload
var upLoadTypeLabel = app.createLabel('File Upload');
var upLoad = (app.createFileUpload().setName('thefile'));
//file upload
var upLoadTypeLabel = app.createLabel('File Upload');
var upLoad = (app.createFileUpload().setName('thefile'));

//Grid layout of items on form
grid.setWidget(0, 0, nameLabel)
    .setWidget(0, 1, nameTextbox)
    .setWidget(1, 0, nationalityLabel)
    .setWidget(1, 1, nationalityTextbox)
    .setWidget(2, 0, emailaddressLabel)
    .setWidget(2, 1, emailaddressTextbox)
    .setWidget(3, 0, donationamountLabel)
    .setWidget(3, 1, donationamountTextbox)
    .setWidget(4, 0, upLoadTypeLabel)
    .setWidget(4, 1, upLoad)
    .setWidget(5, 0, upLoadTypeLabel)
    .setWidget(5, 1, upLoad)
    .setWidget(6, 0, upLoadTypeLabel)
    .setWidget(6, 1, upLoad)
    .setWidget(7, 0, submitButton)
    .setWidget(7, 1, warning)


var cliHandler = app.createClientHandler().forTargets(warning).setVisible(true)
submitButton.addClickHandler(cliHandler);  
panel.add(grid);
app.add(panel);
return app;}

 function doPost(e) {
var app = UiApp.getActiveApplication();  
var Name = e.parameter.Name;
var Nationality = e.parameter.Nationality;
var EmailAddress = e.parameter.emailaddress;
var DonationAmount = e.parameter.donationamount;
  //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');

var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
var lastRow = sheet.getLastRow();
var targetRange = sheet.getRange(lastRow+1, 1, 1, 4).setValues([[Name,Nationality,EmailAddress,DonationAmount]]);
 // data returned is a blob for FileUpload widget
 var fileBlob = e.parameter.thefile;
 var doc = DocsList.createFile(fileBlob);
 return app}

1 个答案:

答案 0 :(得分:0)

  1. 您需要将 DocList Api更新为 DriveApp api

  2. app.createTextBox()。在doGet(e)中的setWidth('150px')。setname({value})参数必须与do.Post(e)中的e.parameter。{value}匹配

  3. 像这样

    在doGet(e)中

        var nameTextbox = app.createTextBox().setWidth('150px').setName('Name');
        var nationalityTextbox = app.createTextBox().setWidth('150px').setName('Nationality');
        var emailaddressTextbox = app.createTextBox().setWidth('150px').setName('emailaddress');
        var donationamountTextbox = app.createTextBox().setWidth('150px').setName('donationamount');
    
    doPost中的

    (e)

    var Name = e.parameter.Name;
    var Nationality = e.parameter.Nationality;
    var EmailAddress = e.parameter.emailaddress;
    var DonationAmount = e.parameter.donationamount;
    

    并且最后创建3个文件上传需要对每个上传方法使用唯一值ex:setName('thefile1'),setName('thefile2'),setName('thefile3')

    以下是经过修改的脚本

    // Script-as-app template.
    var submissionSSKey = '<------Spreadsheet Key ------->';
    
    function doGet(e) {
    var app = UiApp.createApplication().setTitle('HowIMetMyself Registration');
    var panel = app.createFormPanel();
    var grid = app.createGrid(8,2).setId('registrationGrid');
    var nameLabel = app.createLabel('Name');
    var nameTextbox = app.createTextBox().setWidth('150px').setName('Name');
    var nationalityLabel = app.createLabel("Nationality");
    var nationalityTextbox = app.createTextBox().setWidth('150px').setName('Nationality');
    var emailaddressLabel = app.createLabel('Email Address');
    var emailaddressTextbox = app.createTextBox().setWidth('150px').setName('emailaddress');
    var donationamountLabel = app.createLabel('Donation Amount');
    var donationamountTextbox = app.createTextBox().setWidth('150px').setName('donationamount');
    var submitButton = app.createSubmitButton('<B>Submit</B>'); 
    var warning = app.createHTML('<B>PLEASE WAIT WHILE DATA IS UPLOADING<B>').setStyleAttribute('background','yellow').setVisible(false)
    //file upload
    var upLoadTypeLabel = app.createLabel('File Upload');
    var upLoad1 = (app.createFileUpload().setName('thefile1'));
    //file upload
    var upLoadTypeLabel = app.createLabel('File Upload');
    var upLoad2 = (app.createFileUpload().setName('thefile2'));
    //file upload
    var upLoadTypeLabel = app.createLabel('File Upload');
    var upLoad3 = (app.createFileUpload().setName('thefile3'));
    
    //Grid layout of items on form
    grid.setWidget(0, 0, nameLabel)
        .setWidget(0, 1, nameTextbox)
        .setWidget(1, 0, nationalityLabel)
        .setWidget(1, 1, nationalityTextbox)
        .setWidget(2, 0, emailaddressLabel)
        .setWidget(2, 1, emailaddressTextbox)
        .setWidget(3, 0, donationamountLabel)
        .setWidget(3, 1, donationamountTextbox)
        .setWidget(4, 0, upLoadTypeLabel)
        .setWidget(4, 1, upLoad1)
        .setWidget(5, 0, upLoadTypeLabel)
        .setWidget(5, 1, upLoad2)
        .setWidget(6, 0, upLoadTypeLabel)
        .setWidget(6, 1, upLoad3)
        .setWidget(7, 0, submitButton)
        .setWidget(7, 1, warning)
    
    
    var cliHandler = app.createClientHandler().forTargets(warning).setVisible(true)
    submitButton.addClickHandler(cliHandler);  
    panel.add(grid);
    app.add(panel);
    return app;}
    
     function doPost(e) {
    var app = UiApp.getActiveApplication();  
    var Name = e.parameter.Name;
    var Nationality = e.parameter.Nationality;
    var EmailAddress = e.parameter.emailaddress;
    var DonationAmount = e.parameter.donationamount;
      //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');
    
    var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
    var lastRow = sheet.getLastRow();
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 4).setValues([[Name,Nationality,EmailAddress,DonationAmount]]);
     // data returned is a blob for FileUpload widget
     var fileBlob1 = e.parameter.thefile1;
     var fileBlob2 = e.parameter.thefile2;
     var fileBlob3 = e.parameter.thefile3;
     var doc1 = DriveApp.createFile(fileBlob1);
     var doc2 = DriveApp.createFile(fileBlob2);
     var doc3 = DriveApp.createFile(fileBlob3);
     return app
     }