0
votes

I am currently using appscript with HTML service to populate a spreadsheet and upload a couple of files to a google drive. These files are images for which urls are not image links in google drive. Image links are only usable with files on google sites. So I need to convert this from uploading to drive to uploading to a folder on a google sites file cabinet page. All of the info I have found re: attachments to google sites are using the UI app which I am not interested in. I want to use the HTML service. Here is what I have which is fully functional:

Server .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>
1
Hi, welcome to SO. To ensure your question has more visibility, please edit it and add the related language tags (HTML and, I think, Javascript). Thank you! - Fabio says Reinstate Monica
Thanks for the feedback. I think I used the tags most appropriate to the question since it has less to do with language specific syntax and more to do with Google specific functioning. - Lawrence Towers
Sandy thanks for your input. Yes I looked at that, You posted info for creating an attachment that is a link to another resource. I need to use a html form to actually upload the files and upon success to write the URL of the uploaded files to a spreadsheet along with other gathered info. - Lawrence Towers
The code you posted seems to have all the parts needed to do what you want. You will need to provide more information. Do you know how to use console.log('message to print') in the browser, and Logger.log('my variable name: ' + variableName); in the script code to debug the code? Up to what point is the code working? Apps Script documentation - troubleshooting I deleted my last comment to save space. - Alan Wells

1 Answers

0
votes

There is currently no way to add an attachment to a specific folder on a File Cabinet type page of a Google Site using AppScript - SitesApp service does not expose any methods to work with File Cabinet folders in any way.

If you are OK with just uploading a file as attachment to a specific site page without putting it in a folder, then the code for that is pretty straight-forward:

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();
  }
}

Hope this helps!