我正在使用Google表单自动填充Google表格。使用此表格,客户可以告诉我他们何时要预订。我使用了一些代码将我的Google工作表同步到Google日历。
我有2个问题。
(1)每次我运行脚本时,它都会复制该事件。 (2)当添加更多行时,由于我已预先选择了范围,所以它不会同步新事件。 (这是我知道的唯一方法)每次添加新行时,我都无法修改代码!
这是到目前为止的代码:
function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange("P1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var signups = spreadsheet.getRange("J2:L4").getValues();
for (x=0; x<signups.length; x++) {
var shift = signups [x];
var startTime = shift[0];
var endTime = shift[1];
var inspector = shift[2];
eventCal.createEvent(inspector, startTime, endTime);
}
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Synchroniser au calendrier')
.addItem('Sync now', 'scheduleShifts')
.addToUi();
}
答案 0 :(得分:0)
这是一个对话框的示例,该对话框可能与您可以使用的对话框接近。
文件名:aq4.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
google.script.run
.withSuccessHandler(function(vA){
var id='sel1';
var select = document.getElementById(id);
select.options.length = 0;
for(var i=0;i<vA.length;i++) {
select.options[i] = new Option(vA[i],vA[i]);
}
})
.getInspectors();
});
function submitForm(frmData) {
google.script.run
.withSuccessHandler(function(hl){
document.getElementById('status').style.display ='inline';
document.getElementById('status').innerHTML=hl;
})
.createEvent(frmData)
}
console.log('My Code');
</script>
</head>
<body>
<h1 id="main-heading">Event Dialog</h1>
<div id="formDiv">
<form id="myForm">
<br /><input type="text" name="title" value=""/> Title
<br /><input type="datetime-local" name="start" id="dt"/> Start
<br /><input type="datetime-local" name="end" /> End
<br /><select id="sel1" name="inspector"></select> Inspector
<br /><input type="button" value="Submit" onclick="submitForm(this.parentNode)" />
</form>
</div>
<div id="status" style="display: none"></div>
</body>
</html>
文件名:aq1.gs
function createEvent(frmData) {
//Logger.log('title: %s, start: %s, end: %s, inspector: %s ',frmData.title,frmData.start,frmData.end,frmData.inspector);
var rv='Event Not Created';
var cal=CalendarApp.getCalendarById("calendar id");//insert your calendar id
var event=cal.createEvent(frmData.title,new Date(frmData.start),new Date(frmData.end));
if(event) {
event.setDescription(Utilities.formatString('Inspector: %s', frmData.inspector))
rv="Event Created";
}
return rv;
}
//The following function gets the data in column one of the Inspectors sheet or tab in the script container spreadsheet. You can create your own list that way and then you wont have to type them in
function getInspectors() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Inspectors');
var rg=sh.getRange(1,1,sh.getLastRow()-1,1);
var vA=rg.getValues();
return vA;
}
function launchDialog() {
var userInterface=HtmlService.createHtmlOutputFromFile('aq4');
SpreadsheetApp.getUi().showModelessDialog(userInterface, "Event Creation Dialog");
}
这是您可以用来显示日历ID的免费赠品:
function displayCalendarInfo()
{
var cals=CalendarApp.getAllCalendars();
var s='Calendar Information';
for(var i=0;i<cals.length;i++)
{
s+=Utilities.formatString('<br />Name: %s Id: %s', cals[i].getName(),cals[i].getId());
}
s+='<br /><input type="button" value="Close" onClick="google.script.host.close();" />';
var ui=HtmlService.createHtmlOutput(s).setWidth(1200).setHeight(450);
SpreadsheetApp.getUi().showModelessDialog(ui, 'Calendar Data');
}
这也是一个简单的对话框。
这是对话框的外观:
警告:请勿尝试通过google.script.run将Date()从客户端传递到服务器。阅读this,直到您了解它在告诉您什么。
答案 1 :(得分:0)
var existingEvents=Calendar.Events.list(calendarId);
var eventArray=[];
existingEvents.items.forEach(function(e){eventArray.push(e.summary)});
for (x=0; x<signups.length; x++) {
var shift = signups [x];
var startTime = shift[0];
var endTime = shift[1];
var inspector = shift[2];
if(eventArray.indexOf(inspector)==-1){
eventCal.createEvent(inspector, startTime, endTime);
}else{
Logger.log('event exists already');
}
}
使用前请确保enable高级日历服务
var startRow=2;
var startColumn=10; //column J
var rowNumber=spreadsheet.getLastRow()-startRow+1;
var columnNumber=3;
var signups = spreadsheet.getRange(startRow,startColumn,rowNumber,columnNumber).getValues();