Google Forms Automation

Thời gian đọc: ~30 phút

Form Submit Trigger

Khi có người điền Google Form, Apps Script có thể tự động xử lý: gửi email xác nhận, cập nhật Sheet, thông báo cho team, v.v.

Cài đặt: Mở Apps Script từ Form (hoặc linked Sheet) → Triggers (⏰) → Add trigger → chọn onFormSubmit.

function onFormSubmit(e) {
  // e.values: mang cac cau tra loi theo thu tu cau hoi
  // e.namedValues: object { "Cau hoi": ["Tra loi"] }

  const values = e.values;
  const timestamp = values[0];
  const email = values[1];
  const name = values[2];
  const phone = values[3];
  const message = values[4];

  // Kiem tra hop le
  if (!email || !name) {
    Logger.log('Thieu thong tin bat buoc');
    return;
  }

  // Gui email xac nhan cho nguoi dung
  MailApp.sendEmail({
    to: email,
    subject: 'Xac nhan dang ky thanh cong',
    htmlBody: '<h3>Chao ' + name + '!</h3>'
      + '<p>Chung toi da nhan duoc yeu cau cua ban va se lien he trong 24h.</p>'
      + '<p>Noi dung: ' + message + '</p>'
  });

  // Thong bao cho admin
  MailApp.sendEmail({
    to: 'admin@company.com',
    subject: '[Lead moi] ' + name + ' - ' + phone,
    body: 'Ten: ' + name + '\nEmail: ' + email + '\nSo DT: ' + phone + '\nTin nhan: ' + message
  });

  Logger.log('Da xu ly form cua: ' + name);
}

Dùng namedValues — Dễ Đọc Hơn

function onFormSubmit(e) {
  // namedValues an toan hon khi them/xoa cau hoi
  const named = e.namedValues;
  const name = named['Ho va ten'] ? named['Ho va ten'][0] : '';
  const email = named['Email'] ? named['Email'][0] : '';
  const category = named['Loai san pham'] ? named['Loai san pham'][0] : '';

  // Ghi vao sheet phu theo category
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName(category);
  if (!sheet) {
    sheet = ss.insertSheet(category);
  }
  sheet.appendRow([new Date(), name, email]);
}

FormApp API — Tạo & Sửa Form Bằng Code

// Tao form moi hoan toan bang code
function createSurveyForm() {
  const form = FormApp.create('Khao sat khach hang Q1/2026');
  form.setDescription('Vui long danh gia dich vu cua chung toi');

  // Cau hoi ngan
  form.addTextItem()
    .setTitle('Ho va ten')
    .setRequired(true);

  // Cau hoi email
  form.addTextItem()
    .setTitle('Email lien lac')
    .setHelpText('Vi du: abc@gmail.com')
    .setRequired(true);

  // Cau hoi chon mot
  form.addMultipleChoiceItem()
    .setTitle('Danh gia chat luong dich vu')
    .setChoiceValues(['Xuat sac', 'Tot', 'Trung binh', 'Kem'])
    .setRequired(true);

  // Cau hoi chon nhieu
  form.addCheckboxItem()
    .setTitle('Ban biet den chung toi qua dau?')
    .setChoiceValues(['Google', 'Facebook', 'Ban be gioi thieu', 'Khac']);

  // Cau hoi dai
  form.addParagraphTextItem()
    .setTitle('Gop y them');

  Logger.log('Tao form thanh cong: ' + form.getEditUrl());
  return form.getPublishedUrl();
}

Tạo Link Điền Sẵn (Pre-fill)

// Tao URL form voi du lieu dien san — tang ty le phan hoi
function createPrefillUrl(formId, name, email) {
  const form = FormApp.openById(formId);
  const items = form.getItems();

  // Tim entry ID cua tung cau hoi
  // (Xem qua View Source cua form de lay entry.XXXXXX)
  const base = 'https://docs.google.com/forms/d/' + formId + '/viewform';
  const params = '?entry.123456=' + encodeURIComponent(name)
    + '&entry.789012=' + encodeURIComponent(email);

  return base + params;
}

// Gui email voi link pre-fill sau khi mua hang
function sendSurveyAfterPurchase() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const orders = sheet.getDataRange().getValues();

  orders.slice(1).forEach(function(row) {
    const name = row[0];
    const email = row[1];
    const orderId = row[2];
    const surveyUrl = createPrefillUrl('YOUR_FORM_ID', name, email);

    MailApp.sendEmail({
      to: email,
      subject: 'Danh gia don hang ' + orderId,
      htmlBody: '<p>Chao ' + name + '! Vui long <a href="' + surveyUrl + '">danh gia don hang</a></p>'
    });
  });
}
Ứng dụng thực tế: Sau mỗi đơn hàng hoàn thành, tự động gửi email khảo sát với tên khách đã điền sẵn. Tỷ lệ phản hồi tăng 40% so với form trống.