Created
February 19, 2022 03:55
-
-
Save Tomokatsu-Sakamoto/2105ef9f553bad6f7a8ead76e637f471 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; // 変数の宣言を強要 | |
function sheetCopyProtect( ) { | |
const spread = SpreadsheetApp.getActive( ); // 現在のスプレッドシートを取得 | |
const sheet = SpreadsheetApp.getActiveSheet( ); // 現在のシートを取得 | |
let sheetValues = sheet.getDataRange( ).getValues( ); // シートに入力されている内容をすべて取得 | |
// 処理の対象となるスプレッドシートを開く | |
const target = SpreadsheetApp.openById( sheetValues[ 0 ][ 1 ] ); | |
const targetSheet = target.getActiveSheet( ); | |
spread.toast( | |
"スプレッドシート『" + target.getName( ) + "』の" + | |
"シート「" + targetSheet.getName( ) + "」を、" + | |
"指定されたユーザーごとに複製して保護します。" ); | |
// 4列目以降に、処理の対象となるデータが保存されているはず | |
for ( let i = 4 - 1 ; i < sheetValues.length ; i++ ) { | |
if ( sheetValues[ i ][ 0 ] != "" ) { | |
// シートに対象となるデータが入力されていれば、処理する | |
// まずは、シートを複製する | |
let copySheet = targetSheet.copyTo( target ); | |
// シート名を変更する | |
copySheet.setName( sheetValues[ i ][ 0 ] ); | |
// シートを保護する | |
let protctInfo = copySheet.protect( ); | |
protctInfo.addEditor( sheetValues[ i ][ 1 ] ); // 編集者を追加 | |
} | |
else { | |
break; // 入力されている内容がなくなったので、くり返し終了 | |
} | |
} | |
spread.toast( "複製と保護が終了しました。" ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment