2015-09-05 127 views
1

我看到谷歌開發人員發佈這裏注意。 https://developers.google.com/apps-script/releases/ 在2015年8月4日,他們說:「增加了以下方法對電子表格服務,讓腳本來控制‘警告爲本’爲電子表格保護範圍」如何在谷歌電子表格中使用保護setWarningOnly(warningOnly)

* Protection.isWarningOnly() * Protection.setWarningOnly(warningOnly )

我試着做警告基於保護片,與谷歌Apps腳本。(它應該是有一些無保護的細胞)

我想這樣的,但它不工作。

var sheet = SpreadsheetApp.getActiveSheet(); 
var protection = sheet.protect().setDescription('Sample protected sheet').isWarningOnly() 
var unprotected = sheet.getRange('B2:C5'); 
protection.setUnprotectedRanges([unprotected]); 
protection.setWarningOnly(warningOnly) 

有沒有人成功使用warningOnly方法? 請告訴我如何使用這個。

回答

1

有幾種方法來實現這一點。如果你想只設置一個細胞的保護價值,你可以使用以下命令:

function warningOnly() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var cell = sheet.getActiveCell(); 

    // setWarningOnly is a boolean value, which means you have to set 
    // it in the code as true or false. 
    cell.protect().setWarningOnly(true); 
} 

如果你有一個受保護的區域牀單和你想改變他們包括警告,您可以運行細胞的快速測試,找出:

function addNewWarning() { 
    var ss = SpreadsheetApp.getActive(); 

    // Test to see if a cell has a range protected already 
    // If it is protected, remove the old rule and add the warning. 
    // If it's not protected already, skip it. 

    var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE) 
    for(var i=0;i<protections.length;i++) { 
    var protection = protections[i]; 

    // If it's protected and you can edit, remove it and add a warning. 
    if (protection.canEdit()) { 
     protection.remove(); 
     protection.getRange().protect().setWarningOnly(true); 
     Logger.log("Warning added"); 
    } else { 
     Logger.log("Not protected, no warning added"); 
    } 
    } 
} 

我不能找到一種方法來改變警告信息,但它可以讓你在尋找的行爲。希望這可以幫助。

+0

感謝您的幫助!現在我明白了setWarningOnly的工作原理!你是對的,它需要設置「真實」!再次,謝謝你! – idotassadar