How to set values for all columns except one?

妖精的绣舞 提交于 2019-12-14 02:29:36

问题


My code works great, all it does is take events from a spreadsheet and creates them on google calendar. However, one of the columns in my spreadsheet contains a formula. Everytime I run the code, the formula disappears and is replaced by whatever is on that cell at the time.

I know this is where the issue is:

   // Record all event IDs to spreadsheet except for row 7
   range.setValues(data);

How can you write a loop to only apply this from row[0] to row[8] but skipping row [7]?

But here is the full code for reference:

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Export Events",
    functionName : "exportEvents"
  }];
  sheet.addMenu("Update Calendar", entries);
}

//Export events from spreadsheet to calendar//
function exportEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "calendar_ID";
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var title = row[1];           // Second column
    var tstart = new Date(row[2]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[3]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[4];      
    var desc = row[5];
    var complete = row[6];
    var status = row[7];
    var id = row[8];              // Eight column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:"("+status+") "+desc,location:loc}).getId();
      row[8] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setDescription("("+status+") "+desc);
      event.setLocation(loc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, tstart, tstop);
    }
    debugger;
  }
  // Record all event IDs to spreadsheet except for row 7
  range.setValues(data);
}

回答1:


There is no way to write "selectively" using setValues... I suggest you splice your array (vertically) in 2 new arrays and write it with 2 different setValues, leaving the formula's column untouched.

Note that the splice array method splices horizontally (for a 2D array) so you'll have to loop into the first level and do the splicing for each row but working with arrays is very fast so it won't be an issue.

example code :(I took data from a sheet to demonstrate)

function spliceVertically() {
  var data = SpreadsheetApp.getActive().getActiveSheet().getDataRange().getValues();
  Logger.log('data = '+JSON.stringify(data));
  var data1 = [];
  var data2 = [];
  for(var n in data){
    data2.push(data[n].splice(7,1));// cut the row at col 8 and keep 1
    data1.push(data[n].splice(0,6));// cut at 0 and keep 6 , this method cut the array >> get data2 before data1
  }
  Logger.log('data1 = '+JSON.stringify(data1));
  Logger.log('data2 = '+JSON.stringify(data2));
}


EDIT :

to insert it in your code simply use it like this :

  sh.getRange(1,1,data1.length,data1[0].length).setValues(data1);// update col 1 to 6
  sh.getRange(1,8,data2.length,data2[0].length).setValues(data2);// update col 8

Btw, you can also use slice method ... below is a complete test with sheet update :

function spliceVertically() {
  var sh = SpreadsheetApp.getActive().getActiveSheet();
  var range = sh.getDataRange();
  var data = range.getValues();
  Logger.log('data = '+JSON.stringify(data));
  var data1 = [];
  var data2 = [];
  for(var n in data){
    data1.push(data[n].slice(0,8));
    data2.push(data[n].slice(9,10));
  }
  Logger.log('data1 = '+JSON.stringify(data1));
  Logger.log('data2 = '+JSON.stringify(data2));
  sh.getRange(1,1,data1.length,data1[0].length).setValues(data1);
  sh.getRange(1,10,data2.length,data2[0].length).setValues(data2);
}


final edit : full code implemented in yours

since it seems you didn't get it working, below is a full implementation. tested on this sheet

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Export Events",
    functionName : "exportEvents"
  }];
  sheet.addMenu("Update Calendar", entries);
}

//Export events from spreadsheet to calendar//
function exportEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "h22nevo15tm0nojb6ul4hu7ft8@group.calendar.google.com"; //removed link on purpose
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var title = row[1];           // Second column
    var tstart = new Date(row[2]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[3]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[4];      
    var desc = row[5];
    var complete = row[6];
    var status = row[7];
    var id = row[8];              // Eight column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:"("+status+") "+desc,location:loc}).getId();
      row[8] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setDescription("("+status+") "+desc);
      event.setLocation(loc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, tstart, tstop);
    }
    data[i]=row;// update data with row values otherwise it is lost !
  }
  Logger.log(data);
  var data1 = [];
  var data2 = [];
  for(var n in data){
    data1.push(data[n].slice(0,8));
    data2.push(data[n].slice(8,9));
  }
  Logger.log('data1 = '+JSON.stringify(data1));
  Logger.log('data2 = '+JSON.stringify(data2));
  sheet.getRange(1,1,data1.length,data1[0].length).setValues(data1); // write below to check where it writes !!!
  sheet.getRange(1,9,data2.length,data2[0].length).setValues(data2); // change row6 to 1 whan copying in real code !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // Record all event IDs to spreadsheet except for row 7
}


来源:https://stackoverflow.com/questions/25770080/how-to-set-values-for-all-columns-except-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!