Help us understand the problem. What is going on with this article?

運用当番を決めるプログラム

More than 1 year has passed since last update.

組み合わせ決め

kujibiki.java
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Kujibiki{
   static final String DELIMITTER = " & ";
   static final String NEW_LINE = "\n";
   private static JTextArea  jl1;
   private static JLabel  jl2;
   private static JButton  button;
   private static JButton  clearButton;

   public static void main(String[] args) {

      //フレーム表示
      JFrame f = new JFrame("チーム運用当番くじ引きー");

      //各パーツを作る
      jl1 = new JTextArea();
      jl2 = new JLabel("改行区切りで入力してちょんまげ");
      button = new JButton("抽選開始");
      clearButton = new JButton("クリア");

      //イベント登録
      addEvent();

      //コンテンツ区画の取得
      Container cont =  f.getContentPane();
      //パーツをコンテンツに追加する
      cont.add(jl2,BorderLayout.NORTH);
      cont.add(jl1,BorderLayout.CENTER);
      cont.add(button,BorderLayout.EAST);
      cont.add(clearButton,BorderLayout.SOUTH);

      //フレームのサイズを指定
      f.setSize(250, 400);
      //フレームの表示・非表示を指定
      f.setVisible(true);
      //×を押した時の処理
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //描画
      f.show();
   }

   static void addEvent(){
      //クリックイベント登録
      button.addMouseListener(
        new java.awt.event.MouseAdapter(){
          public void mouseClicked(java.awt.event.MouseEvent event){
            //バリデート
            if(isTextNg()){
              jl1.setText("ちゃんと入力してね〜" + NEW_LINE);
              jl1.append("例)" + NEW_LINE + "鈴木" + NEW_LINE + "佐藤");
              return;
            }

            //入力値取得
            java.util.List<String> list = getText();
            //抽選
            int pear = 2;
            java.util.List<String> shuffleList = shuffle1(list,pear);
            //表示
            dispResult(shuffleList);
          }
        }
       );

      clearButton.addMouseListener(
        new java.awt.event.MouseAdapter(){
          public void mouseClicked(java.awt.event.MouseEvent event){
            jl1.setText("");
          }
        }
       );   
   }

   static boolean isTextNg(){
     String strTextArea = jl1.getText().trim();
     return isNullOrEmpty(strTextArea);
   }

   static java.util.List getText(){
     String s = jl1.getText().trim();
     String ss[] = s.split(NEW_LINE);
     java.util.List<String> list = Arrays.asList(ss);
     return list;
   }

   static java.util.List shuffle1(java.util.List<String> list,int pear){
     java.util.List<String> nameList = new ArrayList();
     String buf = null;
     int i = 0;
     int amari = list.size() % pear;

     //組み合わせを抽選
     Collections.shuffle(list);
     //組み合わせ決定
     for(String s : list){
       if(isNullOrEmpty(s)) {
         continue;
       }
       if(i > 0) {
           if(list.size() <  (i + amari + pear)) {
             buf = s + DELIMITTER + buf;
           } else if(i % pear != 0){
             buf = s + DELIMITTER + buf;
             nameList.add(buf);
             buf = "";
           } else {
             buf = s;
           }
       } else {
         buf = s;
       }
       i++;
     }
     nameList.add(buf);

     //順番を抽選
     Collections.shuffle(nameList);
     return nameList;

  }

   static void dispResult(java.util.List<String> nameList){
     String input = jl1.getText().trim();
     jl1.setText("【抽選結果】" + NEW_LINE);
     int i = 1;
     for(String s : nameList){
       jl1.append(String.valueOf(i) + "組目" + NEW_LINE);
       jl1.append(s);
       jl1.append(NEW_LINE + NEW_LINE);
       i++;
     }
     jl1.append("仲良く仕事してね〜" + NEW_LINE);

     jl1.append("【入力値】" + NEW_LINE);
     jl1.append(input);

     return;

  }

   static boolean isNullOrEmpty(String str){
     boolean result = false;
     if (str == null || str.isEmpty()) {
       result = true;
     }
     return result;
   }
}

googleスプレッドシートを元にgoogleカレンダーに登録

calendarUtil.gs
/*
* 実行した日付を元に行を検索し、カレンダを登録するスクリプト
*
*/
function createAllDayEventFromSheetByToday() {
  var targetDate;
  var today = new Date();
  var searchDate = Utilities.formatDate( today, 'JST', 'yyyy/MM/dd');
  var sheet = getSheetInstance('カレンダー');


  //行を特定しヒットした日付の担当者のカレンダーを登録
  for (var i = 1; i <= sheet.getLastRow(); i++){
    targetDate = Utilities.formatDate(sheet.getRange(i,1).getValue(),'JST', 'yyyy/MM/dd');
    if( targetDate == searchDate){
      createAllDayEvent(sheet, today ,i);
    }
  }
  return;
}

/*
* 指定した範囲のカレンダを登録するスクリプト
*
*/
function createAllDayEventFromSheetAll() {
  var sheet = getSheetInstance('カレンダー');
  var startRow = 180;
  var lastRow = sheet.getLastRow();
  for (var i = startRow; i <= lastRow; i++){
    createAllDayEvent(sheet, new Date(sheet.getRange(i,1).getValue()) ,i);
  }
  return;
}


/*
* カレンダを登録する
* @param sheet シートオブジェクト
* @param targetDate 対象日
* @param idx 対象行
*/
function createAllDayEvent(sheet, targetDate, idx) {
  var nameList = new Array();
  var mailList = new Array();
  var title = 'チーム運用当番 ';

  //名前を設定
  var userName = '';
  for (var i = 3; i <= 5; i++){
    userName= sheet.getRange(idx, i).getValue();
    if (userName) nameList.push(userName);
  }

  //メアドを設定
  var userMail = '';
  for (var i = 6; i <= 8; i++){
    userMail= sheet.getRange(idx, i).getValue();
    if (userMail) mailList.push(userMail);
  }

  if(mailList.length > 0){ 
    var cal =  getCalendarInstance('チーム運用当番');
    cal.createAllDayEvent(title+nameList.join(), targetDate, {description:"https://docs.google.com/spreadsheets/d/hogehoge",
                                              guests:mailList.join(),
                                              sendInvites:false});
  }
  return;
}

/*
* 指定した範囲のカレンダを削除するスクリプト
*
*/
function deleteAllDayEvent() {
  var sheet = getSheetInstance('カレンダー');  
  var cal =  getCalendarInstance('チーム運用当番');

  var startRow = 180;
  var lastRow = sheet.getLastRow();
  var events = cal.getEvents( new Date(sheet.getRange(startRow,1).getValue()),  new Date(sheet.getRange(lastRow,1).getValue()));

  for(var i=0; i<events.length; i++){
      events[i].deleteEvent();
  }
  return;
}

/*
* 指定したカレンダー名のオブジェクトを取得
* @param calendarName カレンダ名
* @return CalendarObject カレンダーオブジェクト
*/
function getCalendarInstance(calendarName) {
  var cal = CalendarApp.getCalendarsByName(calendarName);
  return cal[0];
}

/*
* 指定したシート名のオブジェクトを取得
* @param sheetName シート名
* @return SheetObject シートオブジェクト
*/
function getSheetInstance(sheetName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);
  return sheet;
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした