首页 > 解决方案 > 密码保护 Google Apps 脚本

问题描述

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

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});
}

我将非常感谢您提供的任何建议。

标签: google-apps-scriptpasswordspassword-protection

解决方案


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

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();
  }
}

我只提供了一段代码,如果看起来有点奇怪,我很抱歉。我只是不想提供整个代码并让您搜索所有内容。希望有帮助:)


推荐阅读