Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tomokatsu-Sakamoto/a26a47f339387db17ab2138f3ac1c103 to your computer and use it in GitHub Desktop.
Save Tomokatsu-Sakamoto/a26a47f339387db17ab2138f3ac1c103 to your computer and use it in GitHub Desktop.
"use strict";
// 半角英数字と一部記号からランダムな8桁文字列を生成する
//
// ※ https://javascript.programmer-reference.com/js-create-random-string/
function getRndStr( len ) {
//使用文字の定義
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&";
//ランダムな文字列の生成
var result = "";
for( var i = 0 ; i < len ; i++ ) {
result += str.charAt( Math.floor( Math.random( ) * str.length ) );
}
return result;
}
//----------------------------------------------------------------------------
// チェック結果をクリア
function cellClear( ) {
SpreadsheetApp.getActiveSheet().getRange( 'A:D' ).clearContent( );
SpreadsheetApp.getActiveSheet().getRange( 'A1' ).setValue( "Email Address" );
SpreadsheetApp.getActiveSheet().getRange( 'B1' ).setValue( "Password" );
SpreadsheetApp.getActiveSheet().getRange( 'C1' ).setValue( "ユーザー名" );
SpreadsheetApp.getActiveSheet().getRange( 'D1' ).setValue( "実行結果" );
}
//----------------------------------------------------------------------------
// パスワードの自動生成
function passGenerator( ) {
for( var i = 2 ; ; i++ ) {
let eMail = SpreadsheetApp.getActiveSheet().getRange( i, 1 ).getValue( );
if ( eMail != "" ) {
// 「Email Address」が指定されている
SpreadsheetApp.getActiveSheet().getRange( i, 2 ).setValue( getRndStr( 8 ) );
}
else {
// 「Email Address」が指定されていない
break;
}
}
}
//----------------------------------------------------------------------------
// ユーザー名の照会
function nameCheck( ) {
for( var i = 2 ; ; i++ ) {
let eMail = SpreadsheetApp.getActiveSheet().getRange( i, 1 ).getValue( );
if ( eMail != "" ) {
// 「Email Address」が指定されている
try {
var uInfo = AdminDirectory.Users.get( eMail );
// ユーザー情報が取得できるということは、「既に存在している」ということ
SpreadsheetApp.getActiveSheet().getRange( i, 3 ).setValue( uInfo.name.givenName + " " + uInfo.name.familyName + "" );
} catch( e ) {
// ユーザー情報が取得できなかった。何かしらのエラーが発生したのだろう...
SpreadsheetApp.getActiveSheet().getRange( i, 4 ).setValue( e.message );
}
}
else {
// 「Email Address」が指定されていない
break;
}
}
}
//----------------------------------------------------------------------------
// パスワードの再設定
function passUpdate( ) {
for( var i = 2 ; ; i++ ) {
let pMode = SpreadsheetApp.getActiveSheet().getRange( 1, 5 ).getValue( );
let eMail = SpreadsheetApp.getActiveSheet().getRange( i, 1 ).getValue( );
if ( eMail != "" ) {
// 「Email Address」が指定されている
try {
var uInfo = AdminDirectory.Users.get( eMail );
// ユーザー情報が取得できるということは、「既に存在している」ということ
let sPass = SpreadsheetApp.getActiveSheet().getRange( i, 2 ).getValue( );
Logger.log( "passUpdate( ) : " + eMail + " / " + sPass );
// 次回ログイン時にパスワードの変更を
if ( pMode != 0 ) {
// 求める
pMode = true;
}
else {
// 求めない
pMode = false;
}
var data = {
"changePasswordAtNextLogin" : pMode,
"password": sPass,
};
var res = AdminDirectory.Users.update( JSON.stringify( data ), eMail );
Logger.log( "Users.update : " + res );
SpreadsheetApp.getActiveSheet().getRange( i, 4 ).setValue( "OK" );
} catch( e ) {
// ユーザー情報が取得できなかった。何かしらのエラーが発生したのだろう...
SpreadsheetApp.getActiveSheet().getRange( i, 4 ).setValue( e.message );
Logger.log( e );
}
}
else {
// 「Email Address」が指定されていない
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment