我编写了一个简单的onOpen代码,以在多个用户的Google表格上触发。该代码的作用是,它隐藏与其他用户有关的工作表,但仅打开属于该用户的工作表。但是,当我打开工作表时,此代码似乎工作正常,但不会为其他用户触发,而是使工作表保持隐藏状态,原因是onOpen()
代码从未触发。
/** @OnlyCurrentDoc */
function onOpen() {
var app = SpreadsheetApp;
var ProjectSummary = app.getActiveSpreadsheet().getSheetByName("Project Summary (YTD)");
var ProjectSummaryMonth = app.getActiveSpreadsheet().getSheetByName("Project Summary (Monthly)");
var AdminInputs = app.getActiveSpreadsheet().getSheetByName("AdminInputs");
var Project = app.getActiveSpreadsheet().getSheetByName("ProjectInputs");
var TimeTracker = app.getActiveSpreadsheet().getSheetByName("Login");
//User Sheets
var SRSheet = app.getActiveSpreadsheet().getSheetByName("SR");
var DSSheet = app.getActiveSpreadsheet().getSheetByName("DS");
var GESheet = app.getActiveSpreadsheet().getSheetByName("GE");
var JCSheet = app.getActiveSpreadsheet().getSheetByName("JC");
var JISheet = app.getActiveSpreadsheet().getSheetByName("JI");
var MCSheet = app.getActiveSpreadsheet().getSheetByName("MC");
var MQSheet = app.getActiveSpreadsheet().getSheetByName("MQ");
var NSSheet = app.getActiveSpreadsheet().getSheetByName("NS");
var PJSheet = app.getActiveSpreadsheet().getSheetByName("PJ");
var SPSheet = app.getActiveSpreadsheet().getSheetByName("SP");
// Logic Begins Here, we are checking time stamp and choosing only sheets based on if the sheet was active within past 3 hours or not, if it was not we hide that sheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var currDate = new Date();
var currDateH = (currDate/1000)/(60*60);
for(var i=0; i<=14; i++){
for(var j = 2; j<=16; j++){
if(sheets[i].getName()==TimeTracker.getRange("A"+(j)).getValue() && sheets[i].getName()!=="Login" ){
var userdatetime = (TimeTracker.getRange("B"+(j)).getValue()/1000)/(60*60);
var diff = currDateH-userdatetime;
if(diff>=0.5)
{
sheets[i].hideSheet();
}
}
}
}
var SR = ['sr@neovialogistics.com'];
var DS= ['ds@neovialogistics.com'];
var GE = ['Ge@neovialogistics.com'];
var JI = ['jaI@neovialogistics.com'];
var JC = ['jc@neovialogistics.com'];
var MC = ['mc@neovialogistics.com'];
var MQ = ['mq@neovialogistics.com'];
var NS = ['ns@neovialogistics.com'];
var PJ = ['PJ@neovialogistics.com'];
var SP = ['sp@neovialogistics.com'];
if (SR.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
SRSheet.showSheet();
TimeTracker.getRange("B11").setValue(currDate);
}
if (DS.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
DSSheet.showSheet();
TimeTracker.getRange("B2").setValue(currDate);
}
if (GE.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
GESheet.showSheet();
Project.showSheet();
ProjectSummary.showSheet();
ProjectSummaryMonth.showSheet();
TimeTracker.getRange("B3").setValue(currDate);
TimeTracker.getRange("B13").setValue(currDate);
TimeTracker.getRange("B14").setValue(currDate);
TimeTracker.getRange("B15").setValue(currDate);
}
if (JI.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
JISheet.showSheet();
TimeTracker.getRange("B5").setValue(currDate);
}
if (JC.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
JCSheet.showSheet();
Project.showSheet();
ProjectSummary.showSheet();
ProjectSummaryMonth.showSheet();
TimeTracker.getRange("B4").setValue(currDate);
TimeTracker.getRange("B13").setValue(currDate);
TimeTracker.getRange("B14").setValue(currDate);
TimeTracker.getRange("B15").setValue(currDate);
}
if (MC.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
MCSheet.showSheet();
TimeTracker.getRange("B6").setValue(currDate);
}
if (MQ.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
MQSheet.showSheet();
TimeTracker.getRange("B7").setValue(currDate);
}
if (NS.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
NSSheet.showSheet();
Project.showSheet();
ProjectSummary.showSheet();
ProjectSummaryMonth.showSheet();
TimeTracker.getRange("B8").setValue(currDate);
TimeTracker.getRange("B13").setValue(currDate);
TimeTracker.getRange("B14").setValue(currDate);
TimeTracker.getRange("B15").setValue(currDate);
}
if (PJ.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
PJSheet.showSheet();
TimeTracker.getRange("B9").setValue(currDate);
}
if (SP.indexOf(Session.getEffectiveUser().getEmail()) >= 0){
SPSheet.showSheet();
TimeTracker.getRange("B10").setValue(currDate);
}
}
答案 0 :(得分:0)
如果有多个用户同时打开文件,则脚本将导致冲突,因为不可能为不同的用户隐藏不同的工作表
场景:
onEdit
触发器的Apps脚本来同步主电子表格和单个用户电子表格之间的数据onEdit
触发器将在用户电子表格中设置值,反之亦然。如果您还在suer电子表格中实现onEdit
触发器,它将触发用户对主电子表格所做的编辑onEdit
触发