2014-04-09 2 views
0

나는 작은 질문이 있습니다. 내 코드에서는 Google 캘린더에서 약속을 가져옵니다. 그것은 단지 하나의 탭 (Sheet1)에서 작동하기를 원한다는 점을 제외하면 매우 훌륭하게 작동합니다. 정보를 붙여 넣으려면 해당 탭을 어떻게 가리 킵니까?하나의 탭에 정보 붙여 넣기

내가 Sheet2에서 편집 중이면 거기에 정보가 붙여 넣어집니다.

function export_gcal_to_gsheet(){ 

// 
// Export Google Calendar Events to a Google Spreadsheet 
// 
// This code retrieves events between 2 dates for the specified calendar. 
// It logs the results in the current spreadsheet starting at cell A2 listing the events, 
// dates/times, etc and even calculates event duration (via creating formulas in the spreadsheet) and formats the values. 
// 
// I do re-write the spreadsheet header in Row 1 with every run, as I found it faster to delete then entire sheet content, 
// change my parameters, and re-run my exports versus trying to save the header row manually...so be sure if you change 
// any code, you keep the header in agreement for readability! 
// 
// 1. Please modify the value for mycal to be YOUR calendar email address or one visible on your MY Calendars section of your Google Calendar 
// 2. Please modify the values for events to be the date/time range you want and any search parameters to find or omit calendar entires 
// Note: Events can be easily filtered out/deleted once exported from the calendar 
// 
// Reference Websites: 
// https://developers.google.com/apps-script/reference/calendar/calendar 
// https://developers.google.com/apps-script/reference/calendar/calendar-event 
// 

var mycal = "[email protected]"; 
var cal = CalendarApp.getCalendarById(mycal); 

// Optional variations on getEvents 
// var events = cal.getEvents(new Date("January 3, 2014 00:00:00 CST"), new Date("January 14, 2014 23:59:59 CST")); 
// var events = cal.getEvents(new Date("January 3, 2014 00:00:00 CST"), new Date("January 14, 2014 23:59:59 CST"), {search: 'word1'}); 
// 
// Explanation of how the search section works (as it is NOT quite like most things Google) as part of the getEvents function: 
// {search: 'word1'}    Search for events with word1 
// {search: '-word1'}    Search for events without word1 
// {search: 'word1 word2'}  Search for events with word2 ONLY 
// {search: 'word1-word2'}  Search for events with ???? 
// {search: 'word1 -word2'}  Search for events without word2 
// {search: 'word1+word2'}  Search for events with word1 AND word2 
// {search: 'word1+-word2'}  Search for events with word1 AND without word2 
// 
var events = cal.getEvents(new Date("January 12, 2014 00:00:00 CST"), new Date("May 18, 2014 23:59:59 CST"), {search: '-pauze'}); 


var sheet = SpreadsheetApp.getActiveSheet(); 

// Uncomment this next line if you want to always clear the spreadsheet content before running - Note people could have added extra columns on the data though that would be lost 
// sheet.clearContents(); 

// Create a header record on the current spreadsheet in cells A1:N1 - Match the number of entries in the "header=" to the last parameter 
// of the getRange entry below 
var header = [["Client", "Notitie", "Begin", "Eind", "Duur"]] 
var range = sheet.getRange(1,1,1,5); 
range.setValues(header); 


// Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2) 
for (var i=0;i<events.length;i++) { 
var row=i+2; 
var myformula_placeholder = ''; 
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below 
// NOTE: I've had problems with the getVisibility for some older events not having a value, so I've had do add in some NULL text to make sure it does not error 
var details=[[events[i].getTitle(), events[i].getDescription(), events[i].getStartTime(), events[i].getEndTime(), myformula_placeholder]]; 
var range=sheet.getRange(row,1,1,5); 
range.setValues(details); 

// Writing formulas from scripts requires that you write the formulas separate from non-formulas 
// Write the formula out for this specific row in column 7 to match the position of the field myformula_placeholder from above: foumula over columns F-E for time calc 
var cell=sheet.getRange(row,5); 
cell.setFormula('=(HOUR(D' +row+ ')+(MINUTE(D' +row+ ')/60))-(HOUR(C' +row+ ')+(MINUTE(C' +row+ ')/60))'); 
cell.setNumberFormat('.0'); 

} 
} 

답변

0

사용 기능 getSheetByName 예를 들어, 그렇게하기 :

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName("sheet1");