2016-10-29 8 views
0

招待された人が誰でも編集できる表付きのスプレッドシートを作成しようとしています。しかし、ユーザーは、空であるか、彼によって満たされているセルにのみ書き込むことができます。彼は他人の仕事を上書きすることはできません。特定のセルを変更したユーザーを取得します

私はしかしSession.getActiveUser().getEmail()SpreadsheetApp.getActive().getActiveRange().getA1Notation()とトリガーonEditと私は同時に何かが追加された場合、私はできるようにwould't、二人を区別することはできません、プロパティにエディタのメールや、それらの細胞表記の保存を考えていました誰が何をしているのかを知るために...少なくとも、それはそれがどのように機能するかと思う。

しかし、私は非常にクールな回避策:) トリC嘘が見つかりました:あなたは(https://developers.google.com/apps-script/reference/base/session#getActiveUser()ソース)通常のGmailアカウントをお持ちの場合

おかげ

+1

http://stackoverflow.com/questions/40322476/get-the-user-who-clicked-on-the-button-in-the-spreadsheetと同じコメント、\t あなたが使用している場合「通常の」Gmailアカウントでは、スプレッドシートにアクセスするときにユーザーの電子メールを取得することはできません。ビジネスや教育のバージョンでは、 –

+0

@Sergeinsas yepすることができます。 – Wlad

+0

あなたは実際にそれを行うことができます。私はこの非常にクールな回避策を見つけました、私のコメントを参照してください! :) –

答えて

0

Session.getActiveUserは()のonEditトリガーでづけしませんあなた自身と編集者としてオーナーを取り除くことはできないという事実で、したがって、保護された範囲からすべてのエディタを削除した場合は、自分自身をエディタ(および所有者)として割り当てています。

このスクリプトでは、スプレッドシートの所有者が誰もが邪魔する可能性があります。他の人はあなたが望むように動作します:彼らは自分のエントリと空のフィールドだけを編集することができます。

// Test it with colors 
// var edittedBackgroundColor = "RED"; // makes the change visible, for test purposes 
// var availableBackgroundColor = "LIGHTGREEN"; // makes the change visible, for test purposes 

function onEdit(e) { 
    Logger.log(JSON.stringify(e)); 
    var alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().split(""); 
    var columnStart = e.range.columnStart; 
    var rowStart = e.range.rowStart; 
    var columnEnd = e.range.columnEnd; 
    var rowEnd = e.range.rowEnd; 
    var startA1Notation = alphabet[columnStart-1] + rowStart; 
    var endA1Notation = alphabet[columnEnd-1] + rowEnd; 
    var range = SpreadsheetApp.getActive().getRange(startA1Notation + ":" + endA1Notation); 

    if(range.getValue() === "") { 
    Logger.log("Cases in which the entry is empty."); 
    if(typeof availableBackgroundColor !== 'undefined' && availableBackgroundColor) 
     range.setBackground(availableBackgroundColor) 
    removeEmptyProtections(); 
    return; 
    } 

    // Session.getActiveUser() is not accesible in the onEdit trigger 
    // The user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger 
    // Source: https://developers.google.com/apps-script/reference/base/session#getActiveUser() 

    var protection = range.protect().setDescription('Cell ' + startA1Notation + ' is protected'); 
    if(typeof edittedBackgroundColor !== 'undefined' && edittedBackgroundColor) 
    range.setBackground(edittedBackgroundColor); 

    // Though neither the owner of the spreadsheet nor the current user can be removed 
    // The next line results in only the owner and current user being able to edit 

    protection.removeEditors(protection.getEditors()); 
    Logger.log("These people can edit now: " + protection.getEditors()); 

    // Doublecheck for empty protections (if for any reason this was missed before) 

    removeEmptyProtections(); 
} 

function removeEmptyProtections() { 
    var ss = SpreadsheetApp.getActive(); 
    var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); 
    for (var i = 0; i < protections.length; i++) { 
    var protection = protections[i]; 
    if(! protection.getRange().getValue()) { 
     Logger.log("Removes protection from empty field " + protection.getRange().getA1Notation()); 
     protection.remove(); 
    } 
    } 
    return; 
} 

function isEmptyObject(obj) { 
    for(var prop in obj) { 
     if(obj.hasOwnProperty(prop)) 
      return false; 
    } 
    return JSON.stringify(obj) === JSON.stringify({}); 
} 
+0

私はそれを周りに私の頭をラップしていないことを介して迅速な読書の後に率直にそれは素晴らしいですが、それは時間の問題です。しかし、ユーザーがセルの内容を別のセルにコピーすると、宛先セルは緑色になります。 – Wlad

+1

これはコピー・ペーストでも機能します。私はそれを更新しました、フィールドが空の場合にチェックを簡略化しました:range.getValue()=== "" –

関連する問題