从电子表格上传Google Apps:无法引用活动单元格

时间:2014-01-20 11:23:19

标签: event-handling google-apps-script google-sheets

我有这个谷歌应用程序脚本应该

  1. 显示文件上传对话框
  2. 在google drive中存储文件
  3. 将文件的网址写入当前单元格
  4. 除了第3步之外一切顺利,其中更新的单元格始终是第一张纸中的单元格A1。但光标在另一个单元格的第3页上。

    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("Attach file to sheet");
      var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
      var formContent = app.createVerticalPanel();
      form.add(formContent);  
      formContent.add(app.createFileUpload().setName('thefile'));
      formContent.add(app.createSubmitButton('Submit'));
      app.add(form);
      SpreadsheetApp.getActiveSpreadsheet().show(app);
      return app;
    }
    
    function doPost(e) {
      var fileBlob = e.parameter.thefile;
      var doc = DocsList.getFolderById('0B0uw1JCogWHuc29FWFJMWmc3Z1k').createFile(fileBlob);
      var app = UiApp.getActiveApplication();
      var label = app.createLabel('file uploaded successfully');
      var value = '=hyperlink("' + doc.getUrl() + '","' + doc.getName() + '")'
    
      app.add(label);
      app.close();
      SpreadsheetApp.getActiveSheet().getActiveCell().setValue(value);
      return app;
    }
    

    我在doPost()函数之外尝试了SpreadsheetApp.getActiveSheet().getActiveCell().setValue(value);,这在正常上下文中调用时有效。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

this answer判断,无法从doPost函数中获取当前的电子表格/单元格,我的工作方式是通过doGet函数获取它隐藏的字段并通过表单传递。完整的工作示例here