简体   繁体   中英

Ignoring empty cells in Google app scripts function

I wrote this function to bring spreadsheet entries into my calendar. As you see, now I take all entries in the range B5:B30. If I do not fill in all cells in this range, I get an error as my parameters are wrong.

Now if I would be able to only take the values from this range that are not empty, I think all would work. Is there a way to drop empty cells from my range or only execute getValue() and save it in my definition of signups if there is a value?

function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarID = spreadsheet.getRange("C3").getValue();
var eventCal = CalendarApp.getCalendarById(calendarID);
var lr = spreadsheet.getLastRow()

var signups = spreadsheet.getRange("B5:D30").getValues()

for (x=0; x<signups.length;x++)
{
var shift = signups[x];
var startTime = shift[0];
var endTime = shift[1];
var volunteer= shift[2];
eventCal.createEvent(volunteer, startTime, endTime);
}
}

Try this:

  if (volunteer && startTime && endTime) {
    eventCal.createEvent(volunteer, startTime, endTime);
  }

Some of the best resources for learning Google Apps Script include the Beginner's Guide , theNew Apps Script Editor guide , the Fundamentals of Apps Script with Google Sheets codelab, the Extending Google Sheets page , javascript.info and Mozilla Developer Network .

Thanks: This worked:

function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarID = spreadsheet.getRange("C3").getValue();
var eventCal = CalendarApp.getCalendarById(calendarID);
var lr = spreadsheet.getLastRow()

var signups = spreadsheet.getRange("B5:D30").getValues()

for (x=0; x<signups.length;x++)
{
var shift = signups[x];
var startTime = shift[0];
var endTime = shift[1];
var volunteer= shift[2];
if (volunteer && startTime && endTime){
eventCal.createEvent(volunteer, startTime, endTime);
}
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM