When importing Gmail contents into a spreadsheet If there is a specific item that is repeated This is a Google app script where Lastrow is automatically added.
I want to specify the desired column position for each item.
Please help me.
function myFunction()
{
var threads = GmailApp.search('label:extra is:unread');
var messages = GmailApp.getMessagesForThreads(threads);
var sheet = SpreadsheetApp.getActive().getSheetByName('2021');
for(var i=0; i<messages.length; i++)
{
var plainBody = messages[i][0].getPlainBody();
var lastRow = sheet.getLastRow();
const regex=/ex0:(.*)\n(.*)ex1:(.*)\n(.*)ex2:(.*)\n/g;
if(!plainBody.match(regex))
{
continue;
}
var data = plainBody.match(regex).map(x=>
{
var a0 = (/ex0:(.*)/).test(x)? RegExp.$1 : '0';
var a1 = (/ex1:(.*)/).test(x)? RegExp.$1 : '0';
var a2 = (/ex2:(.*)/).test(x)? RegExp.$1 : '0';
return [a0, a1, a2];
});
sheet.getRange(lastRow+1,1,data.length,data[0].length).setValues(data);
}
threads.forEach(function(thread) {
thread.markRead();
Utilities.sleep(100);
});
}
As a guess. Suppose your desired columns are: B, D, F. Then try to change this line
sheet.getRange(lastRow+1,1,data.length,data[0].length).setValues(data);
with this:
sheet.getRange('B' + lastRow).setValue(data[0]);
sheet.getRange('D' + lastRow).setValue(data[1]);
sheet.getRange('F' + lastRow).setValue(data[2]);