
[英]How to append first row data while creating new sheets dynamically in Google Apps Script
[英]Google Apps Script Google Sheets Gmail Processing New Emails Append Row Array
我正在從 gmail 接收電子郵件,使用 jquery 解析它們,然后我想將每封電子郵件中抓取的數據發送到谷歌表中自己的行中。 我的代碼一直運行到我抓取了必要的數據的程度,然后我將它推送到一個數組中。 然后我想檢查現有的谷歌工作表行中是否有任何匹配項,如果沒有,則將數據放入工作表中的行中。
我使用以下帖子作為數組部分的模型,但無法獲得他為我的數據所做的工作: Google 腳本正在重新處理電子郵件
我很感激任何有關將數組轉儲到工作表中的見解。 我將用於檢查重復項的唯一值是人名(變量名),它是電子表格中的第 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
}
}
這是運行后在工作表上的結果 - 只有第一個數組段粘貼在工作表上 - 它沒有粘貼成一行 - 它被粘貼到一列中
10/19/2020 9:51:16 AM
address
Sandra
email@gmail.com
ACTION: Tour Request 10/03/2020 10:00AM
Source website
我無法讓重復的檢查器部分工作(例如,如果該行已經存在,並且我再次處理同一封電子郵件作為測試,即使它是重復的,信息也會作為一行附加到電子表格中)
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);
}
});
用於為電子表格范圍設置值的數組結構是Array[rows][columns]
。
假設您想使用.setValues()
將兩行數據插入到工作表中。
第 1 行的數據長度為 8 個單元格,每個單元格是 v1-v8 中的一個值。 第 2 行的數據長度也是 8 個單元格,但只有 A、B、G 和 H 列有數據。
數組結構應如下所示:
var arr = [
["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"], // row 1
["A-v", "B-v", "", "", "", "", "G-v", "H-v"] // row 2
];
這將需要設置為:
SpreadsheetApp.getActiveSpredsheet()
.getSheetByName("Sheet-name")
.getRange("A1:B8")
.setValues(arr);
這將在工作表中顯示為:
| 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 | | | | | | | | |
---------+--------+--------+--------+--------+--------+--------+--------+--------+
使用嵌套的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);
}
});
我希望這對你有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.