简体   繁体   English

Google Apps 脚本 - 更改一系列 google 表格单元格中的值

[英]Google Apps Script - Changing values in a range of google sheets cells

In the following example I use Apps Script to schedule Google calendar events based on spreadsheet input.在以下示例中,我使用 Apps 脚本根据电子表格输入安排 Google 日历事件。 This code works just fine however I need to tweak it to do a small manipulation on the source sheet as well.这段代码工作得很好,但是我需要调整它以在源表上做一个小的操作。 You can see here that I filter the range to only include rows that have "Pending" value in column D (r[3]).您可以在此处看到,我过滤范围以仅包含 D 列 (r[3]) 中具有“待定”值的行。 However I need to include a line of code in the loop so that after the filtered rows are synced to my Google Calendar the same cell value in column D changes to "Scheduled" for the respective cell.但是,我需要在循环中包含一行代码,以便在过滤后的行同步到我的 Google 日历后,D 列中的相同单元格值会更改为相应单元格的“已计划”。 I have tried following this solution but could not implement it since I am new to JS.我已尝试遵循此解决方案,但由于我是 JS 新手,因此无法实施。

Google Apps Script -.setValue in cell based on for loop matching 基于 for 循环匹配的单元格中的 Google Apps 脚本 -.setValue


function calendarSync() {
var ss = SpreadsheetApp.getActiveSheet();
var calendarId = "My Calendar ID";
var eventCal = CalendarApp.getCalendarById(calendarId);
var eventArray = ss.getRange('A2:I500').getValues().filter(r => r[3] == "Pending Schedule");

for (x=0; x<eventArray.length; x++) {
  var event = eventArray[x];
  var eventName = event[0];
  var startTime = event[1];
  var endTime = event[2];
  
  var exisEvents = eventCal.getEvents(startTime, endTime, {search: eventName}) //prevents creating duplicate events;
  if (exisEvents.length == 0) {
  eventCal.createEvent(eventName, startTime, endTime);
  }
}
}

One simple solution is to change a bit logic of your script.一个简单的解决方案是更改脚本的一些逻辑。 Instead of using filter use an if statement then overwrite the whole range.而不是使用filter使用if语句然后覆盖整个范围。

function calendarSync() {
  var ss = SpreadsheetApp.getActiveSheet();
  var calendarId = "My Calendar ID";
  var eventCal = CalendarApp.getCalendarById(calendarId);
  var eventArray = ss.getRange('A2:I500').getValues();

  for (x = 0; x < eventArray.length; x++) {

    var event = eventArray[x];
    var eventName = event[0];
    var startTime = event[1];
    var endTime = event[2];
    var status = event[3]; // Used to in the following comparison expression instead of filter
    if (status === "Pending Schedule") {
      var exisEvents = eventCal.getEvents(startTime, endTime, {
        search: eventName
      }) //prevents creating duplicate events;
      if (exisEvents.length == 0) {
        eventCal.createEvent(eventName, startTime, endTime);
        eventArray[x][3] = "Scheduled"; // Update the status
      }
    }

  }
  ss.getRange('A2:I500').setValues(eventArray); // Overwrite the source data with the modified array
}

PS If you are using the default runtime instead of var it's better to use const and let , specially when writing complex scripts. PS 如果您使用默认运行时而不是var最好使用constlet ,特别是在编写复杂脚本时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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