【问题标题】:Using Google Appscript/HTML service to upload attachment to Google site使用 Google Apps Script/HTML 服务将附件上传到 Google 网站
【发布时间】:2016-03-27 18:49:34
【问题描述】:

我目前正在使用带有 HTML 服务的 appscript 来填充电子表格并将几个文件上传到谷歌驱动器。这些文件是 url 不是谷歌驱动器中的图像链接的图像。图片链接仅适用于谷歌网站上的文件。因此,我需要将其从上传到驱动器转换为上传到谷歌站点文件柜页面上的文件夹。 我找到的所有信息都是:谷歌网站的附件正在使用我不感兴趣的 UI 应用程序。我想使用 HTML 服务。这是我所拥有的功能齐全的:

服务器 .gs

var dropBoxId = "blablahbalh"; // Drive ID of 'dropbox' folder
var logSheetId = "blahblahbalh"; // Drive ID of log spreadsheet

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

function uploadFiles(formObject) {
  try {
    // Create a file in Drive from the one provided in the form
   var folder = DriveApp.getFolderById(dropBoxId);
   var blob = formObject.myFile;
   var blob2 = formObject.myFile2;
   var file = folder.createFile(blob);
   var file2 = folder.createFile(blob2);
   file.setDescription("Uploaded by " + formObject.myName);

   // Open the log and record the new file name, URL and name from form
   var ss = SpreadsheetApp.openById(logSheetId);
   var sheet = ss.getSheets()[0];
   sheet.appendRow([file.getName(), file.getUrl(), file2.getName(),
   file2.getUrl(), formObject.myName, formObject.actor_unions,   formObject.actor_email, formObject.actor_phone, formObject.actor_website]);

// Return the new file Drive URL so it can be put in the web app output
return file.getUrl();
} catch (error) {
 return error.toString();
  }
 }

HTML

<form id="myForm">
 <input type="text" name="myName" placeholder="Your full name..."/>
  <input type="text" name="actor_unions" placeholder="Union affiliations..."/>
 <input type="text" name="actor_email" placeholder="email Address..."/>
 <input type="text" name="actor_phone" placeholder="phone number..."/>
 <input type="text" name="actor_website" placeholder="Website..."/>
 <input name="myFile" type="file" />
 <input name="myFile2" type="file" />
 <input type="button" value="Submit"
  onclick="google.script.run
      .withSuccessHandler(updateUrl)
      .withFailureHandler(onFailure)
      .uploadFiles(this.parentNode)" />
 </form>

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

  <script>
   function updateUrl(url) {
       var div = document.getElementById('output');
    div.innerHTML = '<a href="' + url + '">Upload successful!</a>';
  }
  function onFailure(error) {
    alert(error.message);
  }
 </script>

 <style>
 input { display:block; margin: 20px; }
 </style>

【问题讨论】:

  • 嗨,欢迎来到 SO。为确保您的问题有更多的可见性,请edit 它并添加相关的语言标签(HTML 和,我认为是 Javascript)。谢谢!
  • 感谢您的反馈。我想我使用了最适合这个问题的标签,因为它与特定语言的语法关系不大,而与谷歌特定的功能关系更大。
  • 桑迪感谢您的意见。是的,我看过了,您发布了创建附件的信息,该附件是指向另一个资源的链接。我需要使用 html 表单来实际上传文件,并在成功后将上传文件的 URL 连同其他收集的信息一起写入电子表格。
  • 您发布的代码似乎包含了执行您想要的操作所需的所有部分。您将需要提供更多信息。你知道如何在浏览器中使用console.log('message to print'),在脚本代码中使用Logger.log('my variable name: ' + variableName);来调试代码吗?代码工作到什么时候? Apps Script documentation - troubleshooting我删除了我的最后一条评论以节省空间。

标签: google-apps-script google-sites


【解决方案1】:

目前无法使用 AppScript 将附件添加到 Google 网站的文件柜类型页面上的特定文件夹 - SitesApp service 没有公开任何以任何方式处理文件柜文件夹的方法。

如果您可以将文件作为附件上传到特定网站页面而不将其放入文件夹中,那么代码非常简单:

function uploadFiles(formObject) {
  try {
    var blob = formObject.myFile;
    var site = SitesApp.getSite("your-site-name-as-in-url");
    // if your site is under a Google Apps hosted domain, then instead use
    // var site = SitesApp.getSite("your-domain", "your-site-name-as-in-url");
    var page = site.getChildByName("name-of-page-as-in-its-url");
    var attachment = page.addHostedAttachment(blob);
    return attachment.getUrl();
  }
  catch(error) {
    return error.toString();
  }
}

希望这会有所帮助!

【讨论】:

  • 不适用于我的代码。单独尝试了您的代码。执行记录:` [16-03-28 18:43:44:775 EDT] 开始执行 [16-03-28 18:43:44:841 EDT] SitesApp.getSite([sites.google.com/a/nyu.edu/lttest4]) [0.059 秒] [ 16-03-28 18:43:44:843 EDT] 执行成功 [0.061 秒总运行时间] code
  • 您是否根据我的代码中的评论更改了var site 声明,因为您位于托管的 Google Apps 域中?在您的情况下,该行应该是:var site = SitesApp.getSite("nyu.edu", "lttest4"); 您是否还发现并更正了我代码中的错字? var page 行不正确;应该是:var page = site.getChildByName("name-of-your-site-page-as-in-its-url");
  • PS:由于您的代码使用了 try/catch,因此执行脚本中不会显示错误 - 您必须自己记录。错误消息到底说了什么?
  • 非常感谢您的反馈。我没有连接到该站点的问题。我得到的错误是:`Cannot call method "addHostedAttachment" of null` 看起来表单没有传递附件。
  • 该表格在用于上传到驱动器时​​绝对有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多