PDA

View Full Version : Inviare Email da foglio Spreadsheet di Google con testo su Word


Likn'_ùs
10-03-2012, 13:50
Salve,
forse non è la sezione più corretta, ma non sapevo dove altro postare! =)
Avrei bisogno di un grandissimo aiuto da chi sa programmare, visto che io non son capace.

Devo inviare delle mail in modo automatico, i dati sono su un foglio excel di google docs, e sono:
Nome squadra
Componenti
Email

In rete ho trovato questa macro:
http://www.cloudtouch.co.uk/2012/02/21/an-introduction-to-google-apps-script-send-emails-using-spreadsheets-and-gmail/


function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();

var menu = [{
name: "Send Email",
functionName: "uiSendEmail"
}];

ss.addMenu("Notifiche", menu);
}

function uiSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
range = range.offset(1, 0, range.getNumRows()-1);

range.getValues().forEach( function( recipient, index, data ){
var body = "Salve " + recipient[0] + ",\nSiamo felici di informare Lei ed il Suo gruppo che la --- Competition sta per avere inizio. Per poter iniziare la sfida seguite il seguente link\nhttp:\\www.sitoweb.it\n\n ";
GmailApp.sendEmail(recipient[0] + " " + recipient[1] + "<" + recipient[2] + ">", "Accesso alla Piattafroma SaxoBank", body);
});
}

Questo però mi permette solamente di inviare un testo compilato direttamente all'interno della macro (evidenziato in rosso) che è molto scomodo! [\n sta per: A capo].

Ora vorrei poter inviare mail, sempre con i dati presenti sul spreadsheet di google, ma utilizzando un documento (word) di google come testo della mail, per avere il testo della mail in HTML, ma mi da sempre errori vari..
La guida è questa: http://www.cloudtouch.co.uk/2012/03/04/an-introduction-to-google-apps-script-part-2-send-emails-using-spreadsheets-gmail-and-a-document-template/

Il codice è il seguente:

function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();

var menu = [{
name: "Send Email",
functionName: "uiSendEmail"
}];

ss.addMenu("Notifiche", menu);
}

function uiSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();

range = range.offset(1, 0, range.getNumRows()-1);
range.getValues().forEach( function( recipient, key, data ) {
var params = {
htmlBody: getHtmlEmail(recipient[0], recipient[1], recipient[2])
};
GmailApp.sendEmail(recipient[0] + " " + recipient[1] + " <" + recipient[2] + ">", "Competition", '', params);
});
}

function getHtmlEmail(forename, surname, email) {
var templateDocId = ScriptProperties.getProperty("EmailTemplateDocId");
var docId = DocsList.getFileById(templateDocId).makeCopy().getId();
var doc = DocumentApp.openById(docId);
var body = doc.getActiveSection();
var html = "";

var keys = {
RECIPIENT_FORENAME: forename,
RECIPIENT_SURNAME: surname,
RECIPIENT_EMAIL: email,
DATE: Utilities.formatDate(new Date(), Session.getTimeZone(), "EE d MMM, yyyy")
};
for ( var k in keys ){
body.replaceText("%" + k + "%", keys[k]);
}

doc.saveAndClose();
html = getDocAsHtml(docId);
DocsList.getFileById(docId).setTrashed(true);

return html;
}

function getDocAsHtml(docId){
var url = 'https://docs.google.com/feeds/download/documents/Export?exportFormat=html&format=html&id=';
var auth = googleOAuth_('docs',url+docId);
return UrlFetchApp.fetch(url+docId,auth).getContentText();
}

function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}


Ma mi da questo errore, e non so proprio cosa fare..
Oops
Cannot capture continuation from JavaScript code not called directly by executeScriptWithContinuations or callFunctionWithContinuations (line 53)


Potreste aiutarmi per cortesia?
Cordialmente L.