将 javascript 对象传递给应用脚本网络应用

Passing a javascript object to an apps script web app

我正在尝试将 javascript 对象传递给我的应用程序脚本 webapp,但我不知道如何传递无法字符串化的对象(如脚本锁或 blob)。例如:

客户端 gs:

function testEmail() {
  var attachment = DriveApp.getFileById("file id");
  var emailBody = 'email body content';

  //send email
  sendEmailFromClient('sendTo@example.com', "test", emailBody, attachment, 'replyTo@example.com');
}

function sendEmailFromClient(sendTo, title, body, attachments, replyTo) {
  var scriptLock = LockService.getScriptLock();
  if(!scriptLock.tryLock(8000)){
    return;
  }

  var formData = {
    'sendTo': sendTo,
    'title': title,
    'body' : body,
    'attachments': attachments,
    'replyTo' : replyTo,
    'scriptLock' : scriptLock
  };

  var options = {
    'method': 'post',
    'payload': formData
  };
      
  Logger.log(UrlFetchApp.fetch('web app url', options).getContentText());
}

网络应用 gs:

function doPost(event) {
  var parameters = event.parameter;
  try {
    var result = Library['sendEmail'].apply(this, [parameters.sendTo, parameters.title, parameters.body, parameters.attachments, parameters.replyTo]);
    parameters.scriptLock.releaseLock();
  } catch (error) {
    Library.troubleshooting(error);
    return ContentService.createTextOutput(error);
  }
  return ContentService.createTextOutput(result);
}

库 gs:

function sendEmail(sendTo, title, body, attachments, replyTo) {
  if (replyTo == undefined) {
    replyTo = gv_accountingEmail;
  }
  if (attachments == undefined) {
    attachments = [];
  }
  var success = false;
    try {
      GmailApp.sendEmail(
        sendTo.valid,
        title,
        "html content only",
        {
          attachments: attachments,
          replyTo: replyTo,
          name: gv_systemBrandName,
          htmlBody: body
        }
      );
      success = true;
    } catch (error) {
      success = error;
    } finally {
      return success;
    }
}

客户端从他们的驱动器中抓取文件,然后将该信息发送到网络应用程序,然后网络应用程序访问库以完成发送电子邮件的操作。 Web 应用程序对客户端没有的库具有特殊权限,这就是为什么我需要将此类数据发送给它的原因。

如果我 运行 来自客户端的上述代码,记录的错误是“TypeError:parameters.scriptLock.releaseLock 不是一个函数”

如果我再次注释掉 // parameters.scriptLock.releaseLock(); 和 运行,我会得到“异常:无效参数:附件”

如果我通过attachment.getBlob()将附件变成blob,那么邮件发送成功,但没有任何附件。

我们如何将这些类型的对象传递给网络应用程序?

谢谢!

试试这个:

  1. Encode the file first to base-64 web-safe
  2. Send it in your web app using post request
  3. Decode the base-64 web-safe to UTF-8 byte array
  4. Convert it to blob.

示例:

客户端:

function myFunction(){
  var url = "webapp url";
  var file = DriveApp.getFileById("file id")
  var headers = {
    "Authorization": "Bearer " + ScriptApp.getOAuthToken()
  };
  var fileName = file.getName();
  var contentType = file.getBlob().getContentType();
  var formData = {
    fileName: fileName,
    contentType : contentType,
    file: Utilities.base64EncodeWebSafe(file.getBlob().getBytes())
  };

  var options = {
    'method' : 'post',
    'payload' : formData,
    'headers': headers,
  };

  UrlFetchApp.fetch(url, options);
}

网络应用程序:

function doPost(e) {
  var file = Utilities.newBlob(Utilities.base64DecodeWebSafe(e.parameter.file),e.parameter.contentType,e.parameter.fileName)
  Library['sendEmail'].apply(this, [file]);
  return ContentService.createTextOutput("test");
}

图书馆:

function sendEmail(attachments) {
  GmailApp.sendEmail(
    "recipient email address here",
    "Test",
    "html content only",
    {
      attachments: [attachments],
    }
  );
}

输出:

参考: