google-apps-script - 密码保护 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});
}
我将非常感谢您提供的任何建议。
解决方案
好的,所以我实际上想出了如何通过提示进行密码系统。这是我所做的,以防将来有人需要它。
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();
}
}
我只提供了一段代码,如果看起来有点奇怪,我很抱歉。我只是不想提供整个代码并让您搜索所有内容。希望有帮助:)
推荐阅读
- javascript - 关于给动态运动元素添加放大效果的问题
- docker - 尝试启动示例项目 che-ide-server-extension 的问题
- amazon-redshift - 红移优化
- arrays - 在 Swift 中创建数组
- reactjs - 使用 React 从 Firebase 加载数据
- android - 微调器模式下的Android DatePicker不显示日期
- android - 我可以在不同的时间段内滑动 android viewpager 吗?
- mysql - 使用 SQL 查询移动特定的用户列表
- hibernate - 使用错误数量或类型的参数调用函数 RTRIM/LTRIM
- mysql - mysql查询周期数据到每个月