如何设置除一个列以外的所有列的值?

时间:2014-09-10 16:03:35

标签: google-apps-script google-sheets google-calendar-api setvalue

我的代码效果很好,它所做的只是从电子表格中获取事件并在Google日历上创建它们。 但是,我的电子表格中的一列包含公式。每次运行代码时,公式都会消失,并被当时该单元格上的任何内容替换。

我知道这就是问题所在:

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

如何编写循环以仅将其应用于行[0]到行[8]但跳过行[7]?

但这里是完整的参考代码:

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 个答案:

答案 0 :(得分:2)

无法使用setValues“选择性地”编写...我建议您将数组(垂直)拼接在2个新数组中,并使用2个不同的setValues编写它,使公式列保持不变。

请注意,拼接数组方法水平拼接(对于2D数组),因此您必须循环到第一级并为每一行进行拼接,但使用数组非常快,因此不会出现问题

示例代码:(我从工作表中获取数据以进行演示)

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));
}

enter image description here


编辑:

将其插入您的代码中只需使用它:

  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

顺便说一下,你也可以使用切片方法...下面是一个完整的测试表更新:

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);
}

enter image description here


最终编辑:在您的

中实施的完整代码

因为看起来你没有让它工作,下面是一个完整的实现。测试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
}