简体   繁体   English

当新行添加到谷歌表而不添加多个事件时,自动运行 function 以在谷歌脚本中创建新事件

[英]automatically run a function to create a new event in google script when a new row is added to google sheet without adding multiple events

I would like to automatically run a function to create a new event in google script when a new row is added to google sheet without adding multiple events.当将新行添加到谷歌工作表而不添加多个事件时,我想自动运行 function 以在谷歌脚本中创建一个新事件。 I just want the the new event to be created.我只想创建新事件。 My script works but it adds ALL the rows from my sheet again instead of just the newest one added.我的脚本有效,但它再次添加了我工作表中的所有行,而不仅仅是添加了最新的行。

 function createCalendarEvent() { let communityCalendar = CalendarApp.getCalendarById("dlv2d6fo8n552173u2ifl7qn9g@group.calendar.google.com") let sheet = SpreadsheetApp.getActiveSheet(); let scheduleEvent = sheet.getDataRange().getValues(); scheduleEvent.splice(0,1); scheduleEvent.forEach(function(entry){ communityCalendar.createEvent(entry[1], entry[2], entry[3]) }); }

I set a trigger to run the function, but like I said it runs the function for the whole sheet not just the newest row (or event) added我设置了一个触发器来运行 function,但就像我说的那样,它为整个工作表运行 function,而不仅仅是添加的最新行(或事件)

In your situation, how about the following modification?在您的情况下,如何进行以下修改?

Modified script:修改后的脚本:

function createCalendarEvent() {
  let communityCalendar = CalendarApp.getCalendarById("dlv2d6fo8n552173u2ifl7qn9g@group.calendar.google.com");
  let sheet = SpreadsheetApp.getActiveSheet();
  let value = sheet.getRange(sheet.getLastRow(), 2, 1, 3).getValues()[0];
  if (value.includes("")) return;
  communityCalendar.createEvent(...value);
}
  • By this modification, the values are retrieved from the last row of the sheet.通过这种修改,可以从工作表的最后一行检索值。

  • When the latest value is row 2, please modify getRange(sheet.getLastRow(), 2, 1, 3) to getRange(2, 2, 1, 3) .当最新值为第 2 行时,请将getRange(sheet.getLastRow(), 2, 1, 3)修改为getRange(2, 2, 1, 3)

  • If the last row of the sheet is different from the columns "B" to "D", please test the following modified script.如果工作表的最后一行与“B”到“D”列不同,请测试以下修改后的脚本。

     function createCalendarEvent() { Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) { // Ref: https://stackoverflow.com/a/44563639 const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious(); return search? search.getRow(): offsetRow; }; let communityCalendar = CalendarApp.getCalendarById("dlv2d6fo8n552173u2ifl7qn9g@group.calendar.google.com"); let sheet = SpreadsheetApp.getActiveSheet(); let value = sheet.getRange(sheet.get1stNonEmptyRowFromBottom(2), 2, 1, 3).getValues()[0]; if (value.includes("")) return; communityCalendar.createEvent(...value); }
  • If you want to use OnEdit trigger, how about the following sample script?如果你想使用 OnEdit 触发器,下面的示例脚本怎么样? Before you use this script, please install OnEdit trigger to the function createCalendarEvent .在使用此脚本之前,请先将 OnEdit 触发器安装到 function createCalendarEvent Ref When you use this script, please edit the cells of the sheet. Ref使用此脚本时,请编辑工作表的单元格。 By this, the script is run by the installable OnEdit trigger.这样,脚本由可安装的 OnEdit 触发器运行。 In this script, please don't directly run the script, because an error occurs.在这个脚本中,请不要直接运行脚本,因为会发生错误。 Please be careful about this.请注意这一点。

     function createCalendarEvent(e) { const sheetName = "Sheet1"; // Please set your sheet name. const { range } = e; const sheet = range.getSheet(); const value = sheet.getRange(range.rowStart, 2, 1, 3).getValues()[0]; if (sheet.getSheetName().= sheetName || value;includes("")) return. let communityCalendar = CalendarApp.getCalendarById("dlv2d6fo8n552173u2ifl7qn9g@group.calendar.google;com"). communityCalendar.createEvent(..;value); }

Reference:参考:

暂无
暂无

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

相关问题 Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet 每当向 Google 表格输入新数据时,如何运行以下 Google Apps 脚本 function? - How to run the following Google Apps Script function whenever new data is entered to Google Sheet? 我想为谷歌工作表创建一个脚本,它将制作一张工作表的多个副本并将新工作表放在活动工作表的左侧 - I want to create a script for google sheets that will make multiple copies of a sheet and place the new sheets to the left of the active sheet Google 表格脚本 - 将 email 添加到 Google 表格时自动发送带有值的值 - Google Sheet script - Automatically send an email with values when they are added to a Google Sheet Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty 火力地堡 | 谷歌表 | 应用脚本 | 每次触发函数时如何将值放在新行上? - Firebase | Google Sheet | App Script | How do I put the value on new row everytime the function is triggered? Google Sheet 上的脚本以运行 Function - Script on Google Sheet to Run Function 发送 Email 用于使用谷歌脚本添加的新行 - Send Email for New Row added using google script 在我创建新工作表之前,Google Apps 脚本会复制工作表 - Google Apps Script copies a sheet before I create a new one Google Apps 脚本:使用边栏创建新工作表 - Google Apps Script: Use the sidebar to create a new sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM