[英]Google Apps Script Google Sheets Gmail Processing New Emails Append Row Array
I'm taking emails from gmail, parsing them using jquery, then I want to send the scraped data from each email into its own row in the google sheet.我正在从 gmail 接收电子邮件,使用 jquery 解析它们,然后我想将每封电子邮件中抓取的数据发送到谷歌表中自己的行中。 I have the code working up to the point where I have the necessary data scraped, and I push it into an array.
我的代码一直运行到我抓取了必要的数据的程度,然后我将它推送到一个数组中。 Then I want to check for duplicates if there are any matches in the existing google sheet rows and if not then put the data into rows in the sheet.
然后我想检查现有的谷歌工作表行中是否有任何匹配项,如果没有,则将数据放入工作表中的行中。
I used the following post as a model for the array part but can't get what he did to work for my data: Google script is reprocessing emails我使用以下帖子作为数组部分的模型,但无法获得他为我的数据所做的工作: Google 脚本正在重新处理电子邮件
I'd appreciate any insights into getting the array to dump into the sheet.我很感激任何有关将数组转储到工作表中的见解。 The unique value I'll be using to check for duplicates would be the person's name (var name) which is column 3 in the spreadsheet
我将用于检查重复项的唯一值是人名(变量名),它是电子表格中的第 3 列
function appendLead() {
var url = 'https://docs.google.com/spreadsheets/d/1zZDKMISf8Hbw7ERfP6h-Q0pztQSMqsN-mHfeM3E_Ywg/edit#gid=0';
var ss = SpreadsheetApp.openByUrl(url);
var leadsData = ss.getSheetByName('Leads Data');
var myLabel = "Buyer Inquiries/1. To Process"; // Name of current label being processed, this label is set via a filter in Gmail
var newLabel = "Buyer Inquiries/2. Processed"; // Name of "New" filter, this label needs to be created in Gmail first
var label = GmailApp.getUserLabelByName(myLabel);
var label2 = GmailApp.getUserLabelByName(newLabel);
var threads = label.getThreads();
var data2 = [];
var newData = [];
for (var i = 0; i< threads.length; i++) {
var message = GmailApp.getMessagesForThread(threads[i])[0];
// move thread from label to label2
label2.addToThread(threads[i]);
label.removeFromThread(threads[i]);
var messageBodyhtml = message.getBody()
var $ = Cheerio.load(messageBodyhtml);
var date = $('span:contains("Date:")').siblings('span').text().trim();
var property = $('span:contains("Regarding:")').siblings('span').text().trim();
var name = $('span:contains("From:")').first().siblings('span').text().trim();
var phone = $('span:contains("Contact Information:")').siblings('span').children().first().text().trim();
var email = $('span:contains("Contact Information:")').siblings('span').children().last().text().trim();
var comments = $('span:contains("Comments:")').siblings('span').text().trim();
var source = $('span:contains("Lead From:")').siblings('span').text().trim();
var array = [date,property,name,phone,email,comments,source]
Logger.log(array);
data2.push([array[i]]);
}
// Compare the information in the data2 array to existing information in the sheet
var data = leadsData.getRange("A2:G").getValues(); //Change this to fit your data ranges
for(i in data2){
var row = data2[i];
var duplicate = false;
for(j in data){
if(row.join() == data[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
if (newData.length){ // runs the below code only if there is newData, this stops an error when newData is empty
leadsData.getRange(leadsData.getLastRow()+1, 1, newData.length, newData[0].length).setValues(newData); //writes newData to end of sheet
}
}
This is the result on the sheet after running - only the first array segment pastes on the sheet - and it's not pasted in as a row - it's pasted in down one column这是运行后在工作表上的结果 - 只有第一个数组段粘贴在工作表上 - 它没有粘贴成一行 - 它被粘贴到一列中
10/19/2020 9:51:16 AM
address
Sandra
email@gmail.com
ACTION: Tour Request 10/03/2020 10:00AM
Source website
I can't get the duplicate checker portion to work (eg if the row already exists, and I process the same email again as a test, the info gets appended as a row in the spreadsheet even if it's a duplicate)我无法让重复的检查器部分工作(例如,如果该行已经存在,并且我再次处理同一封电子邮件作为测试,即使它是重复的,信息也会作为一行附加到电子表格中)
var data = leadsData.getRange("A2:G").getValues(); //Change this to fit your data ranges
data.forEach(function(row) {
var duplicate = false;
data2.forEach(function(row2) {
if (row.join() == row2.join()) {
duplicate = true;
}
});
if (!duplicate) {
newData.push(row);
}
});
The array structure for setting values to a Spreadsheet range is Array[rows][columns]
.用于为电子表格范围设置值的数组结构是
Array[rows][columns]
。
Let's say you want to insert two rows of data to a sheet using .setValues()
.假设您想使用
.setValues()
将两行数据插入到工作表中。
The data for row 1 is 8 cells in length, each cell being a value from v1-v8.第 1 行的数据长度为 8 个单元格,每个单元格是 v1-v8 中的一个值。 The data for row 2 is also 8 cells in length, but only columns A, B, G and H have data.
第 2 行的数据长度也是 8 个单元格,但只有 A、B、G 和 H 列有数据。
The array structure should be as follows:数组结构应如下所示:
var arr = [
["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"], // row 1
["A-v", "B-v", "", "", "", "", "G-v", "H-v"] // row 2
];
And this will need to be set with:这将需要设置为:
SpreadsheetApp.getActiveSpredsheet()
.getSheetByName("Sheet-name")
.getRange("A1:B8")
.setValues(arr);
This will appear in the Sheet as:这将在工作表中显示为:
| A | B | C | D | E | F | G | H |
=========+========+========+========+========+========+========+========+========+
1 | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 |
---------+--------+--------+--------+--------+--------+--------+--------+--------+
2 | A-v | B-v | | | | | G-v | H-v |
---------+--------+--------+--------+--------+--------+--------+--------+--------+
3 | | | | | | | | |
---------+--------+--------+--------+--------+--------+--------+--------+--------+
4 | | | | | | | | |
---------+--------+--------+--------+--------+--------+--------+--------+--------+
Use a nested forEach()
instead of for... in
:使用嵌套的
forEach()
而不是for... in
:
data.forEach(function(row) {
var duplicate = false;
data2.forEach(function(row2) {
if (row.join() == row2.join()) {
duplicate = true;
}
});
if (!duplicate) {
newData.push(row);
}
});
I hope this is helpful to you!我希望这对你有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.