Skip to content

Instantly share code, notes, and snippets.

@HiroshiOkada
Last active March 14, 2016 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HiroshiOkada/d911b45aa32a99e5c0be to your computer and use it in GitHub Desktop.
Save HiroshiOkada/d911b45aa32a99e5c0be to your computer and use it in GitHub Desktop.
// INotifyPropertyChanged のコードスニペットを生成する。 Google App Script (スプレッドシート用)
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'INotifyPropertyChangedスニペット', functionName: 'snipetINotifyPropertyChanged'}
];
spreadsheet.addMenu('スニペット作成', menuItems);
}
// public な プロパティ名から private なフィールド名を作り出す
function privateName(publicName)
{
return publicName[0].toLowerCase() + publicName.substring(1) ;
}
// スニペットを作成し文書に保存する
function snipetINotifyPropertyChanged()
{
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A2:D99");
var values = range.getValues().filter( function(row,index,array) {
return row[0] != 0;
});
var privatefileds = values.map( function(row,index,array) {
return " private " + row[0] + " " +
privateName(row[1]) + " = " +
row[2] + ";";
}).join("\n");
var properties = values.map( function(row,index,array) {
var comment = "";
if (row[3] != "") {
comment = " /// <summary>" + row[3] + "</summary>\n";
}
return comment +
" public " + row[0] + " " + row[1] +
" {\n" +
" get { return " + privateName(row[1]) + "; }\n" +
" set\n" +
" {\n" +
" if (value != " + privateName(row[1]) + ") {\n" +
" " + privateName(row[1]) + " = value;\n" +
" }\n" +
" NotifyPropertyChanged(\"" + row[1] + "\");\n" +
" }\n" +
" }\n";
}).join("\n\n");
var propertyChanged = " public event PropertyChangedEventHandler PropertyChanged;\n\n" +
" private void NotifyPropertyChanged(string propertyName)\n" +
" {\n" +
" if (PropertyChanged != null)\n" +
" {\n" +
" PropertyChanged(this, new PropertyChangedEventArgs(propertyName));\n" +
" }\n" +
" }\n";
var doc = DocumentApp.create("snipetINotifyPropertyChanged");
var body = doc.getBody();
body.appendParagraph(privatefileds);
body.appendHorizontalRule();
body.appendParagraph(properties);
body.appendHorizontalRule();
body.appendParagraph(propertyChanged);
doc.saveAndClose();
Browser.msgBox("次の文書にコードスニペットを作成しました", doc.getUrl(), Browser.Buttons.OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment