繁体   English   中英

Google Apps 脚本(电子表格) - 根据单元格中的条件选择电子表格中的数组

[英]Google Apps Script (Spreadsheet) - selecting an array in spreadsheet based on a condition in cells

我正在尝试 select 来自谷歌表格的数组,以基于该数组创建谷歌日历事件。 下面的代码块运行得很好并完成了工作。 但我希望能够仅 select 在 D 列中具有“选择”值的范围。我知道这可能是一个非常简单的答案,但我是 JS 新手。

function calendarSync() {
    var spreadSheet = SpreadsheetApp.getActiveSheet;
    var eventCal = CalendarApp.getCalendarById(calendarId);
// Below instead of selecting the entire range I only need the rows that have a value of "select" in their D cell.
    var eventArray = spreadSheet.getRange("A1:D100").getValues();
    
    for (x=0; x<eventMatrix.length; x++){
      var calEvent = eventArray[x];
      var eventName = calEvent[0]
      var startTime = calEvent[1];
      var endTime = calEvent[2];
      
      eventCal.createEvent(eventName, startTime, endTime);
    }

在您的情况下,后续修改如何?

从:

    var spreadSheet = SpreadsheetApp.getActiveSheet;
    var eventCal = CalendarApp.getCalendarById(calendarId);
// Below instead of selecting the entire range I only need the rows that have a value of "select" in their D cell.
    var eventArray = spreadSheet.getRange("A1:D100").getValues();

至:

var spreadSheet = SpreadsheetApp.getActiveSheet(); // Please add ()
var eventCal = CalendarApp.getCalendarById(calendarId);
var eventArray = spreadSheet.getRange("A1:D100").getValues().filter(r => r[3] == "select");
  • 通过此修改, eventArray具有select包含在“D”列中的值。

参考:

暂无
暂无

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

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