我正在尝试使用" soundcloud.com"创建一个搜索用户gmail电子邮件的脚本。现在和newer_than:1天。然后我尝试将这些soundcloud网址添加到带有appendRow的电子表格中。我使用正则表达式来阻止网址以及' soundcloud.com'之后的任何内容。直到空白。我还使用函数来解析html。
我一直在"我们很抱歉,发生了服务器错误"运行脚本几秒钟后。这是代码:
function getTextFromHtml(body) {
return getTextFromNode(Xml.parse(body, true).getElement());
}
function getTextFromNode(x) {
switch(x.toString()) {
case 'XmlText': return x.toXmlString();
case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
default: return '';
}
}
function searchy() {
var search = GmailApp.search('"soundcloud.com" newer_than:1d');
for (var i=0; i < search.length; i++) {
var msgs = search[i].getMessages();
for (var j=0; j < msgs.length; j++) {
var body = msgs[j].getPlainBody();
var printthis = getTextFromHtml(body);
var regex = /https?:\/\/soundcloud\.com\/\S*/gi;
var scURL = regex.exec(printthis);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(scURL);
}
}
}
这是执行记录:
[15-03-05 11:16:59:009 CST] Starting execution
[15-03-05 11:16:59:539 CST] GmailApp.search(["soundcloud.com" newer_than:1d]) [0.512 seconds]
[15-03-05 11:16:59:724 CST] GmailThread.getMessages() [0.184 seconds]
[15-03-05 11:16:59:929 CST] GmailMessage.getPlainBody() [0.205 seconds]
[15-03-05 11:16:59:971 CST] Xml.parse([https://soundcloud.com/josef-salvat/josef-salvat-every-night-1
--
(EMAIL SIGNATURE)
, true]) [0.042 seconds]
[15-03-05 11:16:59:973 CST] XmlDocument.getElement() [0 seconds]
[15-03-05 11:16:59:977 CST] XmlText.toXmlString() [0 seconds]
[15-03-05 11:16:59:977 CST] SpreadsheetApp.getActiveSheet() [0 seconds]
[15-03-05 11:17:00:100 CST] Sheet.appendRow([[https://soundcloud.com/josef-salvat/josef-salvat-every-night-1]]) [0.122 seconds]
[15-03-05 11:17:00:280 CST] GmailThread.getMessages() [0.179 seconds]
[15-03-05 11:17:04:388 CST] GmailMessage.getPlainBody() [4.108 seconds]
[15-03-05 11:17:04:428 CST] Xml.parse([https://soundcloud.com/denniskruissen/de-hofnar-dennis-kruissen-ayo-technology-bootleg
--
(EMAIL SIGNATURE)
, true]) [0.039 seconds]
[15-03-05 11:17:04:429 CST] XmlDocument.getElement() [0 seconds]
[15-03-05 11:17:04:431 CST] XmlText.toXmlString() [0 seconds]
[15-03-05 11:17:04:431 CST] SpreadsheetApp.getActiveSheet() [0 seconds]
[15-03-05 11:17:04:432 CST] Sheet.appendRow([null]) [0 seconds]
[15-03-05 11:17:04:434 CST] Execution failed: We're sorry, a server error occurred. Please wait a bit and try again. (line 24, file "Copy of search") [5.406 seconds total runtime]
任何人都可以帮忙吗?另外,有更好的方法吗?我是新手。
谢谢! 尼尔
EDIT 我想到了。不得不把g放在正则表达式函数中。请参阅Henrique的回答:Google Apps Script Regex exec() returning null非常感谢Vincent的帮助!
答案 0 :(得分:0)
似乎regex.exec()返回null,因为没有匹配。
修复函数以防止错误的一种方法:
function searchy() {
var search = GmailApp.search('"soundcloud.com" newer_than:1d');
for (var i=0; i < search.length; i++) {
var msgs = search[i].getMessages();
for (var j=0; j < msgs.length; j++) {
var body = msgs[j].getPlainBody();
var printthis = getTextFromHtml(body);
var regex = /https?:\/\/soundcloud\.com\/\S*/gi;
var scURL = regex.exec(printthis);
if (scURL !== null) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(scURL);
}
}
}
}
来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec