AdWords Скрипт - Тайный Санта

AdWords Скрипт - Тайный Санта

Dmytro Tonkikh - @dtonkikh

Для настройки скрипта, вам понадобится Google Spreadsheet и AdWords аккаунт, но также можете воспользоваться редактором скриптов в Google Docs.

  1. Создаем Google Spreadsheet и заполняем его по примеру - Secret Santa | Public
  2. Вставляем скрипт
function main() {
   var sheet = SpreadsheetApp.openById("1pas9swV0khzX3XvCW5udvDMFZf3H-TLqDCskLuPErmc").getSheetByName("Лист1"); 
  var values = sheet.getRange(1, 1, sheet.getLastRow(), 2).getValues();
  var random = {};
  values.forEach(function (item) {random[item[1]] = [item[0]]});
  var keys = Object.keys(random);
  var unique = [];
//https://stackoverflow.com/questions/21295162/javascript-randomly-pair-items-from-array-without-repeats
if (keys.length % 2 != 0) {
  Logger.log("You must have an even number of names. You currently have " + names.length + " names.");
  } else {
    var arr1 = keys.slice(), // copy array
    var arr2 = keys.slice(); // copy array again
  // shuffle arrays
    arr1.sort(function() { return 0.5 - Math.random();}); 
    arr2.sort(function() { return 0.5 - Math.random();});
    while (arr1.length) {
      var name1 = arr1.pop(), // get the last value of arr1
      name2 = arr2[0] == name1 ? arr2.pop() : arr2.shift();
  //    ^^ if the first value is the same as name1, 
  //      get the last value, otherwise get the first
  unique.push([name1, name2]);
  }
}
unique.forEach(function (item) {sendMail(item[0], random[item[1]])});
}
function sendMail (recipient, secret) { 
  MailApp.sendEmail(recipient, "Ты был избран, чтобы стать секретным Сантой!", "Никому не говори, что ты тайный Санта для " + secret);
}


3. Запускаем скрипт

4. Выбираем подарки!

Report Page