结合google.script.run和javascript函数

问题描述 投票:1回答:1

我正在构建一个带有文件上传功能的自定义“Google Forms”表单。表单使用自定义的“Thankyou”页面(请参阅第1行:iframe)。 我找到了一个需要在Google Script环境中运行的文件上传脚本,它会将文件上传到我的Google云端硬盘。

现在我需要将此上传脚本与我的自定义Google表单相结合。但我不确切知道如何实现这一点,因为需要组合的操作和文件上传必须首先完成才能进入“谢谢”页面。

我试图将它组合起来,看起来像下面的代码。

表格:

<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(submitted)  { picUploadJs(myForm); }"></iframe>

<form id="myForm" action="https://docs.google.com/forms/d/e/xxx/formResponse" target="hidden_iframe" onsubmit="submitted=true;">
     <input placeholder="1234" name="entry.1234" id="user" type="text">
     <label for="user">User:</label>

     <input name="picToLoad" type="file" />

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

   <button type="submit" name="action">Send</button>
</form>

上传脚本:

<script>

  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 = "The File was UPLOADED!";
    window.location='https://thankyoupage/';
  }

</script>

现在这导致:表单输入数据已提交,上传状态文本出现但没有任何反应:“正在上传。请稍候......”。

结果应该是:表单输入数据提交,上传文件到驱动器并重定向到thankyou页面。

编辑:谷歌脚本代码

function doGet(e) {
  return HtmlService.createTemplateFromFile('test')
    .evaluate() // evaluate MUST come before setting the Sandbox mode
    .setTitle('Name To Appear in Browser Tab')
    //.setSandboxMode();//Defaults to IFRAME which is now the only mode available
}

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

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

  var fldrSssn = DriveApp.getFolderById('xxxx');
    fldrSssn.createFile(fileBlob);

  return true;
}

javascript html google-drive-sdk
1个回答
1
投票
  • 单击“发送”按钮时, Google表单工作正常。 document.getElementById('status').style.display = 'inline'的作品。 processForm(frmData)在Google Apps脚本中的功能不起作用。 updateOutput()在Javascript上的功能不起作用。

如果我的理解是正确的,那么这个修改怎么样?请将此视为几个答案中的一个。

Modification points:

  • 在此修改中,使用onclick事件检索文件,而不修改提交到Google表单的脚​​本。
  • 检索到的文件将转换为base64并发送到Google Apps脚本。

修改后的脚本

HTML:

<form id="myForm" action="https://docs.google.com/forms/d/e/xxx/formResponse" target="hidden_iframe" onsubmit="submitted=true;">
  <input placeholder="1234" name="entry.1234" id="user" type="text">
  <label for="user">User:</label>
  <input name="picToLoad" type="file" id="sampleFile" /> <!-- Modified -->
  <div id="status" style="display: none">
  Uploading. Please wait...
  </div>
  <button type="submit" name="action" id="sampleId" >Send</button> <!-- Modified -->
</form>

Javascript:

<script>
  // Below script was added.
  document.getElementById("sampleId").onclick = function(e) {
    e.stopPropagation();
    e.preventDefault();
    var file = document.getElementById("sampleFile").files[0];
    const f = new FileReader();
    f.onload = (e) => {
      const data = e.target.result.split(",");
      const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
      picUploadJs(obj);
    }
    f.readAsDataURL(file);
  };

  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 = "The File was UPLOADED!";
    window.location='https://thankyoupage/';
  }
</script>

Google Apps Script:

function processForm(theForm) {
  var fileBlob = Utilities.newBlob(Utilities.base64Decode(theForm.data), theForm.mimeType, theForm.fileName);
  var fldrSssn = DriveApp.getFolderById('xxxx');
  fldrSssn.createFile(fileBlob);
  return true;
}

注意:

  • 选择文件并单击“发送”按钮后,该文件将发送到Google Apps脚本,并在Google云端硬盘上创建为文件,同时提交Google表单。然后运行Javascript端的updateOutput()
  • 在此修改后的脚本中,使用了blob。因此,用于上载的文件的最大大小为50 MB。请小心这个。

编辑1:

在你的评论,发现When I remove the id=sampleId from the submit button, the Google Form data is submitted correctly。使用此功能,请测试以下修改。

在此修改中,删除了id="sampleId",并使用元素名称触发事件。

HTML:

<form id="myForm" target="hidden_iframe" onsubmit="submitted=true;">
  <input placeholder="1234" name="entry.1234" id="user" type="text">
  <label for="user">User:</label>
  <input name="picToLoad" type="file" id="sampleFile" />
  <div id="status" style="display: none">
  Uploading. Please wait...
  </div>
  <button type="submit" name="action">Send</button> <!-- Modified -->
</form>

Javascript:

var button = document.getElementsByName('action')[0]; // Modified
button.onclick = function(e) { // Modified
  e.stopPropagation();
  e.preventDefault();
  var file = document.getElementById("sampleFile").files[0];
  const f = new FileReader();
  f.onload = (e) => {
    const data = e.target.result.split(",");
    const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
    picUploadJs(obj);
  }
  f.readAsDataURL(file);
};

编辑2:

我更新了HTML和Javascript。请证实。 Google Apps脚本未经过修改。

HTML:

<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(submitted)  { picUploadJs(myForm); }"></iframe>
<form id="myForm" action="https://docs.google.com/forms/d/e/xxx/formResponse" target="hidden_iframe" onsubmit="submitted=true;">
  <input placeholder="1234" name="entry.1234" id="user" type="text">
  <label for="user">User:</label>
  <input name="picToLoad" type="file" id="sampleFile" /> <!-- Modified -->
  <div id="status" style="display: none">
  Uploading. Please wait...
  </div>
  <button type="submit" name="action">Send</button>
</form>

Javascript:

<script>
  // This function was modified.
  function picUploadJs(myForm) {
    var file = document.getElementById("sampleFile").files[0];
    const f = new FileReader();
    f.onload = (e) => {
      const data = e.target.result.split(",");
      const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
      document.getElementById('status').style.display = 'inline';
      google.script.run.withSuccessHandler(updateOutput).processForm(obj);
    }
    f.readAsDataURL(file);
  }

  function updateOutput() {
    var outputDiv = document.getElementById('status');
    outputDiv.innerHTML = "The File was UPLOADED!";
    window.location='https://thankyoupage/';
  }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.