function parseNewOrdersFromGmail() {
// 1. УКАЖИТЕ НАЗВАНИЕ ВАШЕГО ЛИСТА В ТАБЛИЦЕ
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Заказы"); // Убедитесь, что имя листа верное
if (!sheet) {
Logger.log("Лист 'Заказы' не найден!");
return;
}
// 2. УКАЖИТЕ КРИТЕРИЙ ПОИСКА ПИСЕМ В GMAIL
const searchQuery = 'is:unread subject:"Заявка с сайта"'; // Замените тему на свою
const threads = GmailApp.search(searchQuery);
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
if (message.isUnread()) {
const messageDate = message.getDate();
const senderEmail = message.getFrom();
const body = message.getPlainBody();
// 3. ИЗВЛЕКАЕМ ДАННЫЕ ИЗ ПЕРЕМЕННОЙ 'body'
const email = getValue(body, /Email: (.*)/) || senderEmail;
const name = getValue(body, /Имя: (.*)/);
const company = getValue(body, /Компания: (.*)/);
const orderId = getValue(body, /Номер заказа: (.*)/);
const product = getValue(body, /Продукт: (.*)/);
// Добавляем строку в таблицу: дата и время теперь в первом столбце
sheet.appendRow([
messageDate, // Первый столбец — дата и время
email,
name,
company,
orderId,
product
]);
message.markRead();
}
});
});
}
/**
* Вспомогательная функция для извлечения значения.
*/
function getValue(text, regex) {
const match = text.match(regex);
return match ? match[1].trim() : 'не найдено';
}