0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Googleクラウドサービスを利用して時間指定メール送信システムを作る(その4)

Last updated at Posted at 2017-05-16

前へ> <次へ
 書式設定をして解除するまでその設定が継続するように,Docクラスを改良してみます。


1. ドキュメントに書き込む前に書式設定,書き込み後解除

  • 例えば,次のようにして,アンダーラインを引きたい。
アンダーラインをセットして文字を書いてアンダーラインを解除
  log.setUnderline(true);
  log.print("アンダーライン\n");
  log.setUnderline(false);
  • つまり,次のように書くと
書式設定
function main(){
  var log = new Doc(logID);
  log.clear();
  log.print("解答例です\n");
  
  log.setUnderline(true);
  log.print("アンダーライン\n");
  log.setUnderline(false);
  
  log.setBold(true);
  log.print("ボールド\n");
  log.setBold(false);
  
  log.setItalic(true);
  log.print("italic・イタリック\n");//全角文字ではイタリックにならないようだ
  log.setItalic(false);
  
  var bkup = log.getFontSize();
  log.setFontSize(20);
  log.print("フォントサイズ20\n");
  log.setFontSize(bkup);

  var bkup = log.getForegroundColor();
  log.setForegroundColor("#ff0000");//red
  log.print("赤色\n");
  log.setForegroundColor(bkup);
  
  var bkup = log.getBackgroundColor();
  log.setBackgroundColor("#ffff00");//yellow
  log.print("背景が黄色\n");
  log.setBackgroundColor(bkup);
  
}
  • こんな風になって欲しい。

キャプチャ.PNG

  • GASのライブラリでは,ドキュメントのボディのテキストのx文字目からy文字目までアンダーラインを引く,というメソッドが用意されている。それを利用する。
  • 次のGASプログラムを実行すると,上の実行結果になる。
  • 複数の書式をセットしても反映される。
  • **********は,logドキュメントのIDに置き換える。
書式設定を追加したDocクラスの実行例プログラム
var logID = "**********";

function main(){
  var log = new Doc(logID);
  log.clear();
  log.print("解答例です\n");
  
  log.setUnderline(true);
  log.print("アンダーライン\n");
  log.setUnderline(false);
  
  log.setBold(true);
  log.print("ボールド\n");
  log.setBold(false);
  
  log.setItalic(true);
  log.print("italic・イタリック\n");//全角文字ではイタリックにならないようだ
  log.setItalic(false);
  
  var bkup = log.getFontSize();
  log.setFontSize(20);
  log.print("フォントサイズ20\n");
  log.setFontSize(bkup);

  var bkup = log.getForegroundColor();
  log.setForegroundColor("#ff0000");//red
  log.print("赤色\n");
  log.setForegroundColor(bkup);
  
  var bkup = log.getBackgroundColor();
  log.setBackgroundColor("#ffff00");//yellow
  log.print("背景が黄色\n");
  log.setBackgroundColor(bkup);
  
}

//////////Docクラスの定義開始(コンストラクタとメンバ関数で構成)
//Docクラスのコンストラクタの記述
Doc = function(id){
  this.ID = id;
  this.doc = DocumentApp.openById(this.ID); 
  this.body = this.doc.getBody();
  this.docText = this.body.editAsText();
  this.st = 0;//属性を適用する最初の文字start
  this.ed = 0;//属性を適用する最後の文字end
  this.bold = false;
  this.underline = false;
  this.italic = false;
  this.fontSize = this.docText.getFontSize();//デフォルトのフォントサイズをセット
  this.foregroundColor = this.docText.getForegroundColor();//デフォルトの文字色をセット
  this.backgroundColor = this.docText.getBackgroundColor();//デフォルトの背景色をゲット
}
//Docクラスのメンバ関数の定義開始
//メソッドprintの定義,テキスト追加
Doc.prototype.print = function(str){
  this.st = this.body.getText().length;//元の文字数
  this.docText.appendText(str);//文字数の追加
  this.ed += str.length;//追加後の文字数//this.ed = this.body.getText().length;でもよい

  this.docText.setBold(this.st,this.ed-1,this.bold);
  this.docText.setUnderline(this.st,this.ed-1,this.underline);
  this.docText.setItalic(this.st,this.ed-1,this.italic);
  this.docText.setFontSize(this.st,this.ed-1, this.fontSize);
  this.docText.setForegroundColor(this.st,this.ed-1, this.foregroundColor);
  this.docText.setBackgroundColor(this.st,this.ed-1, this.backgroundColor);
  
  this.st = this.ed;
}
//メソッドreplaceの定義,文字列置き換え
Doc.prototype.replace = function(src,dst){
  this.body.replaceText(src,dst);
}
//メソッドclearの定義,全消去
Doc.prototype.clear = function(){
  this.body.clear();
}
//メソッドgetIDの定義,ファイルIDを返す
Doc.prototype.getID = function(){
  return this.ID;
}
//メソッドsetBoldの定義
Doc.prototype.setBold = function(bold){
  this.bold = bold;
}
//メソッドsetForegroundColorの定義
Doc.prototype.setForegroundColor = function(color){
  this.foregroundColor = color;
}
//メソッドgetForegroundColorの定義
Doc.prototype.getForegroundColor = function(){
  return this.foregroundColor;
}
//メソッドsetBackgroundColorの定義
Doc.prototype.setBackgroundColor = function(color){
  this.backgroundColor = color;
}
//メソッドgetBackgroundColorの定義
Doc.prototype.getBackgroundColor = function(){
  return this.backgroundColor;
}
//メソッドsetFontSizeの定義
Doc.prototype.setFontSize = function(size){
  this.fontSize = size;
}
//メソッドgetFontSizeの定義
Doc.prototype.getFontSize = function(){
  return this.fontSize;//現在のフォントサイズをゲット
}
//メソッドsetUnderlineの定義
Doc.prototype.setUnderline = function(underline){
  this.underline = underline;
}
//メソッドsetItalicの定義
Doc.prototype.setItalic = function(italic){
  this.italic = italic;
}
//////////Docクラスの定義終了
  • logを残すのにここまで凝る必要はないので,今後は次のクラスを使う。
今後使うDocクラス
//////////Docクラスの定義開始(コンストラクタとメンバ関数で構成)
//Docクラスのコンストラクタの記述
Doc = function(id){
  this.ID = id;
  this.doc = DocumentApp.openById(this.ID); 
  this.body = this.doc.getBody();
  this.docText = this.body.editAsText();
}
//Docクラスのメンバ関数の定義開始
//メソッドprintの定義,テキスト追加
Doc.prototype.print = function(str){
  this.docText.appendText(str);
}
//メソッドreplaceの定義,文字列置き換え
Doc.prototype.replace = function(src,dst){
  this.body.replaceText(src,dst);
}
//メソッドclearの定義,全消去
Doc.prototype.clear = function(){
  this.body.clear();
}
//メソッドgetIDの定義,ファイルIDを返す
Doc.prototype.getID = function(){
  return this.ID;
}

 次回は,メール送信するプログラムを作ります。
前へ> <次へ


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?