将文件上传到电子表格时显示“已弃用UiApp。请改用HtmlServices”

时间:2019-07-18 08:36:22

标签: google-apps-script google-sheets

这是昨天才发生的。

我使用此脚本将文档上传到电子表格的单元格,直到今天我遇到了错误

  

UiApp已被弃用。请改用HtmlService。

我该如何解决?

我的代码:

// upload document into google spreadsheet
// and put link to it into current cell

function onOpen(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var menuEntries = [];
  menuEntries.push({name: "File...", functionName: "doGet"});
  ss.addMenu("Attach ...", menuEntries);
}

function doGet(e) {
  var app = UiApp.createApplication().setTitle("upload attachment into Google Drive");
  SpreadsheetApp.getActiveSpreadsheet().show(app);
  var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
  var formContent = app.createVerticalPanel();
  form.add(formContent);  
  formContent.add(app.createFileUpload().setName('thefile'));

  // these parameters need to be passed by form
  // in doPost() these cannot be found out anymore
  formContent.add(app.createHidden("activeCell", SpreadsheetApp.getActiveRange().getA1Notation()));
  formContent.add(app.createHidden("activeSheet", SpreadsheetApp.getActiveSheet().getName()));
  formContent.add(app.createHidden("activeSpreadsheet", SpreadsheetApp.getActiveSpreadsheet().getId()));
  formContent.add(app.createSubmitButton('Submit'));
  app.add(form);
  SpreadsheetApp.getActiveSpreadsheet().show(app);
  return app;
}

function doPost(e) {
  var app = UiApp.getActiveApplication();
  app.createLabel('saving...');
  var fileBlob = e.parameter.thefile;
  var doc = DriveApp.getFolderById('0B3-f0r3K_lCRRzJuRGI3Y2xxxx').createFile(fileBlob);
  var label = app.createLabel('file uploaded successfully');

  // write value into current cell
  var value = 'hyperlink("' + doc.getUrl() + '";"' + doc.getName() + '")'
  var activeSpreadsheet = e.parameter.activeSpreadsheet;
  var activeSheet = e.parameter.activeSheet;
  var activeCell = e.parameter.activeCell;
  var label = app.createLabel('file uploaded successfully');
  app.add(label);
  SpreadsheetApp.openById(activeSpreadsheet).getSheetByName(activeSheet).getRange(activeCell).setFormula(value);
  app.close();
  return app;
}

2 个答案:

答案 0 :(得分:0)

UiApp类自2014年起就已弃用[1],最近他们刚刚将其关闭(7月15日)[2]。

现在,您必须使用HTML服务类[3] [4]。

我有一个与您的代码相似的代码(从表单将文件上传到云端硬盘),您可以从此处开始,需要将脚本绑定到电子表格并创建一个Index.html,在其中显示您的表格:

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_center">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
  <form id="myForm" >
    Select File:
    <input type="file" name="file" accept="*" /><br>
    File name: <input type="text" name="fileName" /><br><br>
    Row position: <input type="number" name="position" /><br><br>
    <input type="button" value="Upload" onclick="upload(this.parentNode);" />
  </form>
  <script>
   window.onload=func1;

  function func1() {
   document.getElementById('myForm').addEventListener('submit', function(event) {
            event.preventDefault();
          });  
  }

   function upload(obj){
       google.script.run.withSuccessHandler(close).withFailureHandler(close).uploadFile(obj);
   }   

   function close(e) {
       console.log(e);
       google.script.host.close();
   }

  </script>
</body>
</html>

Code.gs

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('File')
      .addItem('Attach...', 'showForm')
      .addToUi();  
}

function showForm() {
  var html = HtmlService.createHtmlOutputFromFile('Index');
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(html, 'Upload File');
}

function uploadFile(obj) {
  var newFileName = obj.fileName;
  var rowNum = obj.position;
  var blob = obj.file;

  var upFile = DriveApp.getRootFolder().createFile(blob).setName(newFileName);
  var fileUrl = upFile.getUrl();

  var urlCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(rowNum,5);
  urlCell.setValue('=HYPERLINK("' + fileUrl + '","' + newFileName + '")');

  return "It worked!";    
}

[1] https://developers.google.com/apps-script/reference/ui/ui-app

[2] https://developers.google.com/apps-script/guides/support/sunset

[3] https://developers.google.com/apps-script/guides/html/

[4] https://developers.google.com/apps-script/reference/html/

答案 1 :(得分:0)

非常感谢您的帮助。效果很好。

关于您的代码,我还有其他问题。 -如何使脚本在文件名之后设置文件名。 -如何制作脚本以上传到所选单元格。 -如何设置文件存储文件夹。

非常感谢。