Google Scripts返回未定义

时间:2016-05-02 16:23:36

标签: google-apps-script google-form

我的目标是创建一个门户网站,我可以接受文件上传到以提交者名字命名的文件夹,然后将其名称记录到电子表格中。

“uploadFiles()”函数上传并组织上传到新文件中就好了。但是,当我运行“saveToSpreadsheet()”函数时,它会发布一个“未定义”而不是从我的电子表格中提取任何表单数据。

我是一个相当新的JavaScript,这是我在Google Apps脚本中工作的第一个项目,请原谅我是一个新手,当我问:什么导致功能之间缺乏沟通?

HTML表单

<form id="myForm">
<input type="text" name="artistName" placeholder="First and last name">
<input type="text" name="artTitle" placeholder="Title of submission">
<input type="text" name="yearProduced" placeholder="Year created">
<input type="text" name="medium" placeholder="Medium">

<input type="file" name="myFile">
<input type="submit" value="Upload File" 
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>

<div id="output"></div>

<script>
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }
</script>

Google脚本文件

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

  try {
    var dropbox = form.artistName.replace(/\s+/g, '-').toLowerCase();
    var folder, folders = DriveApp.getFoldersByName(dropbox);
    var artistName = form.artistName;
    var artTitle = form.artTitle;
    var yearProduced = form.yearProduced;
    var medium = form.medium;


    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Created by " + artistName + ". " + artTitle + " in " + yearProduced + " in a medium of " + medium);

    return "File uploaded successfully. Thanks, " + artistName + "!";

    saveToSpreadsheet(artistName);
}    
    catch (error) {

    return error.toString();
  }
}

function saveToSpreadsheet(data) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var j = sheet.getLastRow();

  var cell = sheet.getRange(1, 1);
  var cellValue = sheet.getSheetValues(1, 1, 1, 1);

  cell.setValue(data);

  Logger.log(data);
}

编辑(这最终解决了我的问题)

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

  try {
    var dropbox = form.artistName.replace(/\s+/g, '-').toLowerCase();
    var folder, folders = DriveApp.getFoldersByName(dropbox);
    var artistName = form.artistName;
    var artTitle = form.artTitle;
    var yearProduced = form.yearProduced;
    var medium = form.medium;
    var xyz = saveToSpreadsheet(artistName);


    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Created by " + artistName + ". " + artTitle + " in " + yearProduced + " in a medium of " + medium);

    return "File uploaded successfully. Thanks, " + artistName + "!";

    saveToSpreadsheet(xyz);
}    
    catch (error) {

    return error.toString();
  }
}

function saveToSpreadsheet(data) {
  var ss = SpreadsheetApp.openById('mysheetID');
  var sheet = ss.getSheets()[0];

  var j = sheet.getLastRow();

  var cell = sheet.getRange(1, 1);
  var cellValue = sheet.getSheetValues(1, 1, 1, 1);

  cell.setValue(data);

  Logger.log(data);
}

1 个答案:

答案 0 :(得分:1)

我使用此代码创建一个HTML Bootstrap页面,用户将文件上传到Drive Folder,命名为用户名并登录电子表格。也许这有助于你。

Codigo.gs

   function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('subirCorreo.html')
            .setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

function uploadFiles(form) {

  try {

    // FOLDER ID
    var id= "FOLDERID";

    var carpeta = DriveApp.getFolderById(id);

    var blob = form.myFile;    
    var file = carpeta.createFile(blob);    

    file.setDescription("Subido por " + Session.getActiveUser().getEmail());

    // Esta linea es por si quiere tipo enlace
    // return "El archivo se subio correctamente: <br> <br>" + "<a href="+file.getUrl()+">"+file.getUrl()+"</a>" ;

    return "El archivo se subio correctamente: <br> <br>" + file.getUrl();

  } catch (error) {
    return error.toString();
  }

}

function uploadFilesMail(form) {

  try {

    var id= "FOLDERID";

    var carpeta = DriveApp.getFolderById(id);

    var correo = form.myEmail;

    var blob = form.myFile;    
    var file = carpeta.createFile(blob).setName("Archivo de: "+correo);    

    file.setDescription("Subido por " + Session.getActiveUser().getEmail());

    // Esta linea es por si quiere tipo enlace
    // return "El archivo se subio correctamente: <br> <br>" + "<a href="+file.getUrl()+">"+file.getUrl()+"</a>" ;

    return "El archivo se subio correctamente: <br> <br>" + file.getUrl();

  } catch (error) {
    return error.toString();
  }

}

subir.html

<!-- Se incluye bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<style>
  form { text-align: center ; }
  input  { margin: auto;}
  div { text-align: center ;font-weight: bold; color:#3644FF;  }
</style>

<script>

  function subirArchivo() {
    google.script.run
       .withSuccessHandler(fileUploaded)
       .uploadFiles(document.getElementById("form"));
    return false;
  }

  function fileUploaded(status) {
    document.getElementById('form').style.display = 'none';
    document.getElementById('resultado').innerHTML = status;
  }

</script>

<form id="form">

 <h1> Subir archivo </h1> 

 <input type="file" name="myFile"> <br>

  <input class="btn btn-primary" type="submit" id="uploadFile" value="Subir archivo" 
         onclick="this.value='Subiendo..';subirArchivo();">

</form>

<br>
<br>
<br>
<br>

<!-- Here the results of the form submission will be displayed -->
<div id="resultado">

</div>