如何使用谷歌应用脚​​本

时间:2015-07-22 19:24:05

标签: google-apps-script google-email-settings-api

我已经在google apps脚本中创建了一个脚本,该脚本将google doc的内容读入gmail的草稿消息中。但是,它并没有附加用户的签名。

所以我的计划是检索签名,然后附加到google doc的内容,然后输入草稿消息。

我发现在这里有用于检索用户gmail签名的信息:https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings,但我在尝试在现有脚本中实现它时遇到了麻烦。

我该怎么办? (当前脚本如下)

function doGet() {
createDraft()
return HtmlService.createHtmlOutput('<b>Your catering email template can now be found in your <a href="https://mail.google.com/mail/#drafts">Drafts</a> folder!</b>');
}

function createDraft() {

var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

var doc = DocumentApp.openById('1fsRMxtLx3IBEYvmVy9W8uHLw3Uf2OIh4L7ZSxpkixbY');

var body = doc.getBody();

var mbody = body.getText();



var raw = 
  'Subject: Catering Proposal\r\n' + 
  'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\r\n' + '\r\n' +
  mbody + '\r\n' +
  '--1234567890123456789012345678--\n';

var draftBody = Utilities.base64Encode(raw);

Logger.log(draftBody);

var params = {method:"post",
            contentType: "application/json",
            headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
            muteHttpExceptions:true,
            payload:JSON.stringify({
              "message": {
                "raw": draftBody
              }
            })
           };

var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
Logger.log(resp.getContentText());
}

我非常感谢能提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

用户签名由单独的API处理,而不是由Gmail API处理。 您需要首先添加范围:

import UIKit

class Level1: UIViewController {    

    var timerCount = 0
    var timmerRunning = false
    var timer = NSTimer()

    @IBOutlet weak var timerLabel: UILabel!

    func Counting() {
        timerCount += 1
        timerLabel.text = "\(timerCount)"
    }

    @IBAction func startButton(sender: UIButton) {
        if !timmerRunning {
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
            timmerRunning = true
        }        
    }

    @IBAction func stopButton(sender: UIButton) {
        if timmerRunning {
            timer.invalidate()
            timmerRunning = false
        }
    }

    @IBAction func restartButton(sender: UIButton) {        
        timerCount = 0
        timerLabel.text = "0"        
    }
}

然后使用GET检索签名

https://apps-apis.google.com/a/feeds/emailsettings/2.0/

答案 1 :(得分:1)

现在有一种更简单的方法可以解决这篇文章:

Apps Script to get the users signature

基本上:

prepare(forSegue:..:))