1

我想要獲取表單提交電子表格來創建全天Google日曆事件。 的問題是,我的電子表格有多個ARRAYFORMULA在它因爲代碼使用:GAS如何使用getDataRange和getLastRow當工作表包含動態電子表格中的全列數組表單

var sheet = SpreadsheetApp.getActiveSheet(); 
var rows = sheet.getDataRange(); 
var numRows = rows.getNumRows(); 
var values = rows.getValues(); 
var lr = rows.getLastRow(); 

這是因爲ARRAYFORMULA騙過.getDataRange到他們計數行,而不提交尚未

計算整個工作表全碼:

var calendarId = CalendarApp.getCalendarById("xxxxxxxxxx"); 
//below are the column ids of that represents the values used in the spreadsheet (these are non zero indexed) 

//Column containg the Start Date/Time for the event 
var startDtId = 2; 
//Column containg the End Date/Time for the event 
var endDtId = 2; 
//Column containg the First Part of the Title for the event (In this case, Shift Area) 
var areaId = 9; 
//Column containg the Second part of the Title for the event (In this case, Shift) 
var shiftId = 3; 
//Column containg the Second part of the Title for the event (In this case, Name) 
var nameId = 7; 
//Column containg the Third part of the Title for the event (In this case, Reason) 
var reasonId = 13; 
//Column containg the Description for the event (In this case, Phone) 
var phoneId = 8; 
//Column containg the Time Stamp for the event (This will always be 1) 
var formTimeStampId = 1; 
//Column containg the Location for the event 
var addressId = 14; 


function getLatestAndSubmitToCalendar() { 
//Allow access to the Spreadsheet 
var sheet = SpreadsheetApp.getActiveSheet(); 
var rows = sheet.getDataRange(); 
var numRows = rows.getNumRows(); 
var values = rows.getValues(); 
var lr = rows.getLastRow(); 
//Removed setting of Hour and Minute for the Start and End times as they are all day 
var startDt = sheet.getRange(lr,startDtId,1,1).getValue(); 
var endDt = sheet.getRange(lr,endDtId,1,1).getValue(); 
//Create an addition to the Description to included who added it and when 
var subOn = "This shift was added: "+sheet.getRange(lr,formTimeStampId,1,1).getValue()+" by: "+sheet.getRange(lr,nameId,1,1).getValue()+" - Phone: "+sheet.getRange(lr,phoneId,1,1).getValue(); 
//Setting the Comments as the description, and adding in the Time stamp and Submision info 
var desc = sheet.getRange(lr,reasonId,1,1).getValue()+", "+sheet.getRange(lr,nameId,1,1).getValue()+" is scheduled to ride on the "+sheet.getRange(lr,shiftId,1,1).getValue()+" in "+sheet.getRange(lr,areaId,1,1).getValue()+<br/>+subOn; 
//Create the Title 
var title = sheet.getRange(lr,areaId,1,1).getValue()+"-"+sheet.getRange(lr,shiftId,1,1).getValue()+"["+sheet.getRange(lr,reasonId,1,1).getValue()+"] - "+sheet.getRange(lr,nameId,1,1).getValue(); 
//Run the Create event Function 
createEvent(calendarId,title,startDt,desc); 
}; 

function createEvent(calendarId,title,startDt,endDt,desc) { 
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxxxxxxxxx"); 
var sheet = SpreadsheetApp.getActiveSheet(); 
var rows = sheet.getDataRange(); 
var numRows = rows.getNumRows(); 
var values = rows.getValues(); 
var lr = rows.getLastRow(); 
var start = new Date(startDt); 
var end = new Date(endDt); 
var subOn = "This shift was added: "+sheet.getRange(lr,formTimeStampId,1,1).getValue()+" by: "+sheet.getRange(lr,nameId,1,1).getValue()+" - Phone: "+sheet.getRange(lr,phoneId,1,1).getValue(); 
var desc = sheet.getRange(lr,reasonId,1,1).getValue()+", "+sheet.getRange(lr,nameId,1,1).getValue()+" is scheduled to ride on the "+sheet.getRange(lr,shiftId,1,1).getValue()+" in "+sheet.getRange(lr,areaId,1,1).getValue()+<br/>+subOn; 
//Manually set the Location, this can be modified to be dynamic by modifying the code if need be 
var loc = sheet.getRange(lr,addressId,1,1).getValue(); 

//Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites 
var event = cal.createAllDayEvent(title, start, { 
description : desc, 
location : loc 
}); 
}; 

請幫助:)

+0

參見: http://stackoverflow.com/questions/17632165/determining-the-last-row-in-a-single-column-google-apps-script。 –

+0

我其實已經讀過這個問題,但我不認爲我可以使用這個答案,因爲我的工作表是一個表單提交,所以當每個提交的範圍發生變化時,定義的範圍就不足以滿足要求。 – KyleM

回答

0

其實我已經找到了解決工作對此。我保留上面的.getLastRow()路線,而不是在表格中使用數組公式,我再次使用.getLastRow()並通過.setFormula()添加我的公式。這使我可以擦除數組,因此.getDataRange()現在可以正確地僅獲取具有數據的行,而不是由於數組而導致的所有行。

爲了將正確的順序放入正確的位置,我將包含公式輸入的函數設置爲.onFormSubmit觸發器,並從該函數的末尾調用日曆事件生成器。 這現在工作很好。

// Declare the columns needed to access for formula input first and set to their index 

var columnId = 3; 

// Now set the formulas 

function formulaInput() { 
var ss = SpreadsheetApp.getActiveSpreadsheet() 
var sheet = ss.getSheetByName('Sheet 1') 
var range = sheet.getDataRange() 
var last = range.getLastRow() 


//This sets a formula in the columns of the last row of data that used to have an array formula sheet-side (Using the onFormSubmit trigger) 

sheet.getRange(last,columnId,1,1).setFormula('= insert former array without the arrayformula prefix') 

// Now use this to initiate the calendar event from the original code in question which ensures the formula input occurs 1st and subsequently triggers the calendar event after ALL the deaired data is present. 

return getLatestAndSubmitToCalendar() 

} 
相關問題