Title1

Title2

Title3

11-1 程式碼

 

function getFormResponse() {  
  //由表單-Id-開啟表單
  var form = FormApp.openById("表單-Id");
  
  //取得表單名稱
  var formTitle = form.getTitle();
  
  //取得表單回覆內容
  var formResponse = form.getResponses();   
  
  //處理表單上問答內容
  for(var i = 0; i < formResponse.length ; i++){
    
    //取得表單上的項目
    var itemResponses = formResponse[i].getItemResponses();
    
    //取得表單上「收集電子郵件地址」
    var itemRespondentEmail = formResponse[i].getRespondentEmail();
    //Logger.log(itemRespondentEmail);
    
    //傳送訊息設定
    var itemContext = formTitle + "\n 填報者電子郵件 : " + itemRespondentEmail + "\n";        
      
    //----傳給日曆變數----
    var calendar = [];
    
    //將表單每一項問答組成訊息
    for(var j = 0; j < itemResponses.length ; j++)
    {  
      //----日曆----
      var v = itemResponses[j].getItem().getTitle();
      calendar[v] = itemResponses[j].getResponse();      
      //----日曆end----
      
      //取得問題標題
      itemContext += itemResponses[j].getItem().getTitle();    
      
      //取得問題回覆內容
      itemContext += " : " + itemResponses[j].getResponse() + "\n";
    }    
    
    //取得填寫表單時間
    itemContext += "\n填寫時間 : " + formResponse[i].getTimestamp() + "\n\n";
    
    //送出表單到Line
    sendToLine(itemContext);    
    
    //----寫入日曆----
    setCalendar(calendar);
    
  }
  
  //刪除回應問題
  form.deleteAllResponses();
}

function sendToLine(message){
  //填入Line-Notify-權杖
  var token = "Line-Notify-權杖";
  
  var options =
  {
      method  : "post",
      payload : "message=" + message,
      headers : {"Authorization" : "Bearer "+ token},
      muteHttpExceptions : true
  };  

  UrlFetchApp.fetch("https://notify-api.line.me/api/notify",options);
}


function setCalendar(calendar) {
  Logger.log(calendar);
  
  //日曆ID
  var calId = "日曆ID";    
  
  //設定新增的行程標題
  var title = calendar['房間編號'] + " - " + calendar['客戶姓名'];
   
  //開始日期
  var startDate = calendar['入住日期'];
  startDate = new Date(startDate.replace(/-/g, "/"));//轉換日期格式 
  
  //結束日期(必需多一天)
  var endDate = calendar['退房日期'];
  endDate = new Date(endDate.replace(/-/g, "/"));//轉換日期格式   
  //endDate=endDate.setDate(endDate.getDate()+1);//假如沒有結束日期
  endDate=new Date(endDate);
  
  var description = "房間編號: " + calendar['房間編號'] + "\n";
  description += "客戶姓名: " + calendar['客戶姓名'] + "\n";  
  description += "客戶電話: " + calendar['客戶電話'] + "\n";    
  description += "停車券: " + calendar['停車券'] + "\n";   
  description += "訂金: " + calendar['訂金'] + "\n";
  description += "尾款: " + calendar['尾款'] + "\n";
  description += "經手人員: " + calendar['經手人員'] + "\n";
  
  //顏色
  var color = 11;
  if(calendar['房間編號'] == "601"){
    var color = 1;
  }
  if(calendar['房間編號'] == "602"){
    var color = 2;
  }
  if(calendar['房間編號'] == "603"){
    var color = 3;
  }
  
  //----------------------//
  
  //透過ID取得日曆
  var cal = CalendarApp.getCalendarById(calId);
  
  //建立新的日曆行程
  var even = cal.createAllDayEvent(title, startDate, endDate, {description:description});
  
  //沒有結束日期的全天事件
  //var even = cal.createAllDayEvent(title, startDate, {description:description});

  //設定形成顏色
  even.setColor(color);
  
  //取得形成的ID
  //Logger.log(even.getId());
}