将Google Apps脚本移至v8文件上传已从侧边栏停止工作 [英] Moving google apps script to v8 file upload stopped working from sidebar

查看:67
本文介绍了将Google Apps脚本移至v8文件上传已从侧边栏停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个按预期工作的脚本.但是将脚本移至v8会使脚本无法正常工作.而且我找不到解决方法.

I have a script running that works as intended. But moving the script to v8 makes the script not working. And I can't find a solution.

我已经清理了脚本并在旧引擎中对其进行了测试,并且仍然可以运行,但是在v8中仍然无法运行.

I've cleaned up the script and tested it in the old engine and still works, but in v8 still doesn't working.

这是电子表格中的侧边栏,用于将单个文件上传到我的Google驱动器.

It's a sidebar in a spreadsheet where to upload a single file to my google drive.

这是HTML文件中的基本

Here the basic in the Html file

<body>
<h1>File Uploader</h1>
<form>
    <input type="file" name="myFile" id="file">
    <br>
    <input class="blue" type="button" id="submitBtn" value="Upload File" onclick="uploadthis(this.parentNode)">

</form>

<input type="button" value="Close" onclick="google.script.host.close()" />

<script>

function uploadthis(fileForm){

google.script.run
.uploadFiles(fileForm)
}



</script>

</body>

这里是简化的gs

function uploadContract() {
  var html = HtmlService.createHtmlOutputFromFile('ContractUpload').setTitle('Kontrakt upload').setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}

function uploadFiles(data){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sStamdata = ss.getSheetByName('Stamdata_New');
var contractFolderId = sStamdata.getRange('D60').getValue(); 
var idag = Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd");
var title = sStamdata.getRange('D52').getValue();

var file = data.myFile;
var folder = DriveApp.getFolderById(contractFolderId);
var createFile = folder.createFile(file);
createFile.setName(idag+" - KONTRAKT - "+title);

}

推荐答案

这个答案怎么样?请认为这只是几个可能的答案之一.

How about this answer? Please think of this as just one of several possible answers.

我可以确认您的问题是否也是如此.在这种情况下,我认为启用V8后,当使用google.script.run将对象发送到Google Apps脚本端时,可能无法解析该表单对象.尽管我认为这可能会在将来的更新中进行修改,但作为当前解决方法,我还是建议将上传的文件作为字节数组发送到GAS端.

I could confirm about the same situation of your issue. In this case, I think that when V8 is enabled, the form object might not be able to be parsed when the object is sent to Google Apps Script side with google.script.run. Although I think that this might be modified in the future update, as the current workaround, I would like to propose to send the uploaded file to GAS side as the byte array.

修改脚本后,它如下所示.

When your script is modified, it becomes as follows.

请如下修改uploadthis.

function uploadthis(fileForm){
  const file = fileForm.myFile.files[0];
  const fr = new FileReader();
  fr.onload = function(e) {
    const obj = {
      // filename: file.name,  // In your script, the filename is given at GAS side. So I removed this.
      mimeType: file.type,
      bytes: [...new Int8Array(e.target.result)]
    };
    google.script.run.withSuccessHandler((e) => console.log(e)).uploadFiles(obj);
  };
  fr.readAsArrayBuffer(file);
}

Google Apps脚本端:Code.gs

请按如下所示修改uploadFiles.

Google Apps Script side: Code.gs

Please modify uploadFiles as follows.

function uploadFiles(data){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sStamdata = ss.getSheetByName('Stamdata_New');
  var contractFolderId = sStamdata.getRange('D60').getValue(); 
  var idag = Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd");
  var title = sStamdata.getRange('D52').getValue();

  var file = Utilities.newBlob(data.bytes, data.mimeType, idag+" - KONTRAKT - "+title);  // Modified
  var folder = DriveApp.getFolderById(contractFolderId);
  var createFile = folder.createFile(file);
  return createFile.getId();  // Added
}

注意:

  • 在上述修改中,当文件被上传时,文件被转换为字节数组并将其发送到GAS端.然后,从字节数组创建文件.并返回文件ID.您可以在控制台上看到它.
    • FileReader()
    • newBlob(data, contentType, name)

    如果我误解了您的问题,而这不是您想要的方向,我深表歉意.

    If I misunderstood your question and this was not the direction you want, I apologize.

    这篇关于将Google Apps脚本移至v8文件上传已从侧边栏停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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