HTMLService将变量传递给HTML Picker并再次返回

时间:2018-06-05 03:08:27

标签: google-apps-script

我有这个工作代码。这是从电子表格上的菜单运行的。 它成功地允许我选择文件,然后将它们移动并重命名。

我遇到的问题是:我希望文件的名称前缀为" Photo - "," Agreement - "或&#34 ;条件 - "

我在想的是每个文件类型都会有一个菜单项。选择此菜单项会将正确的变量传递给html文件,可能会使用.append设置,然后让html找到它并通过doSomething函数将其传回,以便我的文件可以获得正确的前缀。

有更好的方法吗?我搜索并搜索无济于事。 谢谢你的帮助。



function showDrivePicker() {
  var html = HtmlService.createHtmlOutputFromFile('pickerDrive.html')
      .setWidth(600)
      .setHeight(425)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi().showModalDialog(html, 'Select files');
}






function doSomething(fileId, photos) {
  // do an operation in Drive with the fileId
  var file = DriveApp.getFileById(fileId);
  var fileName = file.getName();
  var fileDate = file.getDateCreated();
  var fileURL = file.getUrl();
  
  copyAndFileDocument(fileId, fileName, "move");
  
  return fileName;
}






<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
  <script>
    var DEVELOPER_KEY = 'ABC';
    var DIALOG_DIMENSIONS = {width: 600, height: 425};
    var pickerApiLoaded = false;
    function onApiLoad() {
      gapi.load('picker', {'callback': function() {
        pickerApiLoaded = true;
      }});
     }

    function getOAuthToken() {
      google.script.run.withSuccessHandler(createPicker)
          .withFailureHandler(showError).getOAuthToken();
    }

    function createPicker(token) {
      if (pickerApiLoaded && token) {
        var picker = new google.picker.PickerBuilder()
            .addView(new google.picker.DocsView().setParent('root').setIncludeFolders(true))
            .addView(new google.picker.DocsView().setIncludeFolders(true).setOwnedByMe(true))
            .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
            .hideTitleBar()
            .setOAuthToken(token)
            .setDeveloperKey(DEVELOPER_KEY)
            .setCallback(pickerCallback)
            .setOrigin(google.script.host.origin)
            .setSize(DIALOG_DIMENSIONS.width - 2,
                DIALOG_DIMENSIONS.height - 2)
            .build();
            
        picker.setVisible(true);
      } else {
        showError('Unable to load the file picker.');
      }
    }

    function pickerCallback(data) {
      var action = data[google.picker.Response.ACTION];
      var files = "";
      if (action == google.picker.Action.PICKED) {
        for(var i in data[google.picker.Response.DOCUMENTS]){
          var doc = data[google.picker.Response.DOCUMENTS][i];
          var id = doc[google.picker.Document.ID];
          var url = doc[google.picker.Document.URL];
          var title = doc[google.picker.Document.NAME];
          
          files = files + '<br>' + '<b>You chose:</b><br>Name: <a href="' + url + '">' + title +
            '</a><br>ID: ' + id;
          
              var fileId = data.docs[i].id;
              google.script.run
              .withSuccessHandler(useData) 
              .doSomething(fileId, "Photos"); 
           }
           document.getElementById('result').innerHTML = files;
         
           
      } else if (action == google.picker.Action.CANCEL) {
        document.getElementById('result').innerHTML = 'Picker canceled.';
      }
      function useData(data) {
      console.log({message: 'Function Input', initialData: data});
      }
    }

    function showError(message) {
      document.getElementById('result').innerHTML = 'Error: ' + message;
    }
    
  </script>
</head>
<body>
  <div>
    <button onclick='getOAuthToken()'>Select a file</button>
    <p id='result'></p>
  </div>
  <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案