密码保护 Google Apps 脚本

Password Protect Google Apps Script

所以我在这里为我的 google 工作表创建了一个小脚本。由于 google 工作表不允许您在单个工作表上使用密码保护,我想知道是否有一种方法可以使用密码保护我的脚本,以便只有某些人可以使用它。这是我的代码。

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Custom Menu')
      .addItem('Record', 'Record')
      .addItem('Cancelation', 'Cancel')
      .addToUi();
}

function Record() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nightly Stats'),
    row = sheet.getLastRow()
    range = sheet.getRange("A3:G3");
  sheet.insertRowAfter(row);
    range.copyTo(sheet.getRange(row + 1, 1), {contentsOnly:true});
}

如果您能提供任何建议,我将不胜感激。

好的,所以我实际上想出了如何通过提示来建立密码系统。这是我所做的,以防将来有人需要它。

function Cancel() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();

  // first prompt
  var presult = ui.prompt(
    "Please Enter the Password to Use this Feature.",
    ui.ButtonSet.OK_CANCEL);

  var password = "Test";
  var pbutton = presult.getSelectedButton();
  var ptext = presult.getResponseText();

  // User clicked "OK" on first prompt
  if (pbutton == ui.Button.CANCEL) {
    ui.alert('The Process Was Ended.');
    } else if (pbutton == ui.Button.CLOSE) {
      ui.alert('The Process Was Ended.');
    } else if (ptext != password) {
      Password();   
    } else {
      "Insert whatever action you would want them to do after the password works here"
    }
  }

function Password() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert("The Password is Incorrect. Retry?",
      ui.ButtonSet.OK_CANCEL);

  if (response == ui.Button.CANCEL) {
    ui.alert("The Process Was Ended.");
  } else if (response == ui.Button.CLOSE) {
    ui.alert("The Process Was ended.");
  } else {
    Cancel();
  }
}

我只给出了一段代码,如果看起来有点奇怪,请见谅。我只是不想给出整个代码并让您搜索所有内容。希望有所帮助:)

这是一个技巧,但对免费帐户用户很有用! 使用 onOpen() 函数和下面的小代码。

var inputPassword;
function checkPassword(){  
  
  var refAddress = 'https://script.google.com/d/' + ScriptApp.getScriptId() + '/edit';
  var curAddress = refAddress;
  Logger.log(curAddress);  
  
  var referPassword = 1234;

  if( refAddress == curAddress){
      while(referPassword != inputPassword){
      inputPassword = Browser.inputBox('[Password Check]', '[Input Password!]', Browser.Buttons.OK_CANCEL);
    }
  }
}

有关详细信息,请参阅 my blog

所以你想保护脚本本身? 将脚本托管在不同的位置并使用库: https://developers.google.com/apps-script/guides/libraries