在 Google Docs Add On 中将信息传递给服务器端功能

Passing information to server-side function in a Google Docs Add On

我正在开发一个基于 Google 的 Quickstart tutorial 的 Google 文档插件。我正在尝试更改教程中附加组件的工作流程以附加新页面,然后在该新页面上插入翻译,而不是逐行工作流程。

我有一个在单个文档中运行的脚本,但我很难将它移动到附加组件架构。我认为这与将选择从客户端 JS 传递到执行翻译的服务器端脚本有关。

这是翻译脚本

function translate(origin, dest, savePrefs) {
  Logger.log('Starting the script');
  if (savePrefs == true) {
    var userProperties = PropertiesService.getUserProperties();
    userProperties.setProperty('originLang', origin);
    userProperties.setProperty('destLang', dest);
    Logger.log(origin,dest);
  }

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  // Add a page break for the translated material.
  body.appendPageBreak();

  // Get the number of elements in the document
  var elements = body.getNumChildren();
  Logger.log('Got the page elements');

  // Use the number to loop through each element in the document.
  for( var i=0;i<elements;i++) {
   var element = body.getChild(i).copy();
    var type = element.getType();
    Logger.log('Element Types were successful. Starting tests.');    

    // Test each type for a child element and run script based on the result
    // Images are nested in a paragraph as a child, so the second `if` makes
    // sure there is no image present before moving to the next paragraph.
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      if(element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
        var img = element.asParagraph().getChild(0).asInlineImage().getBlob();
        body.appendImage(img);
      } else if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
        var drawing = element.asParagraph().copy();
        body.appendParagraph(drawing);
      } else {
        var text = element.asParagraph().getText();
        Logger.log(text);
        var spn = LanguageApp.translate(text, origin, dest);
        body.appendParagraph(spn);
      }
    } else if(type == DocumentApp.ElementType.TABLE) {
      element.asTable().copy();
      body.appendTable(element);
    } else if(type == DocumentApp.ElementType.LIST_ITEM) {
      var list = element.asListItem().getText();
      body.appendListItem(LanguageApp.translate(list, origin, dest));
    }
  }

客户端JS为:

$(function() {
    $('#run-translation').click(loadPreferences);
    google.script.run(runTranslation)
  });

function runTranslation() {
    this.disabled = true;
    var origin = $('input[name=origin]:checked').val();
    var dest = $('input[name=dest]:checked').val();
    var savePrefs = $('#save-prefs').is(':checked');
    google.script.run
      .runTranslation(origin, dest, savePrefs);
  }

如果我将翻译中使用的语言硬编码到服务器端脚本中,它就可以工作。但是,一旦我尝试使用单选按钮中的变量,它就不会 运行。我在控制台中没有看到任何错误,也无法从编辑器中使用 运行 脚本来检查日志。我该如何调试这段代码?

从客户端调用服务器端函数Javascript

google.run:

你有一个小语法错误
google.script.run(runTranslation)

应该是:

google.script.run
             .withFailureHander(failFunc) // Optional
             .withSuccessHander(succFunc) // Optional
             .serverFunction(optionalParams...);

这两个处理程序方法从您的客户端分配回调函数 JavaScript 在服务器端函数成功或失败的情况下调用。在这两种情况下,客户端函数都是作为唯一参数提供的。

您要与之通信的服务器端功能就好像它本身就是一个方法一样,带有要跨越鸿沟传递的可选参数。

最简单的情况是:

google.script.run
             .translate(origin, dest, savePrefs);

(您在发布的代码中有 runTranslation,但服务器端函数被命名为 translate()...我认为这是正确的。)

现在,这个可能解决不了你所有的问题,所以你明智地询问调试...

在 Google Apps 脚本中调试异步客户端/服务器代码

提供的调试环境不足以调试这种客户端/服务器交换。用于异步执行的调试器也存在弱点 - 您可以在 .

中阅读更多相关信息

在这种情况下进行调试的最简单工具是将日志写入电子表格

/**
 * Write message and timestamp to log spreadsheet.
 * From: 
 */
function myLog( message ) {
  var ss = SpreadsheetApp.openById( logSpreadsheetId );
  var logSheet = ss.getSheetByName("Log") || ss.insertSheet("Log");
  logSheet.appendRow([ new Date(), message );
}

您可以从您的客户端Javascript这样调用它:

google.script.run.myLog( "CLIENT: " + message );

这是一种基本方法,但您可以通过使用实用函数和 BetterLog 库对其进行更多扩展。在我的博客文章 Did you know? (You can log to a spreadsheet from client JavaScript!)

中查看更多相关信息