粘贴数据时不会触发onEdit [英] onEdit not triggering when pasting data

查看:31
本文介绍了粘贴数据时不会触发onEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用onEdit触发器在云端硬盘中创建一个文件夹.在字段中键入数据时有效,但在粘贴数据时不会触发.已更新,其中包含建议的更改,但仍不会触发粘贴数据.

Using onEdit trigger to create a folder in Drive. Works when typing data into field, but does not trigger when I paste data. Updated with recommended changes, still does not trigger with pasted data.

function createSpreadsheetEditTrigger() {
  const ss = SpreadsheetApp.openById('1IzR2ceVKG8SSTDHMx-N6NCuzzUHGyAOHHsykT7r-xcg');
  ScriptApp.newTrigger('addFolder')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

function addFolder(e) {
  // helps with verifying where data was entered
  // in the sheet
  const myNewColumn = e.range.getColumn()
  const myNewRow = e.range.getRow()
  const value = e.range.getValues().flat().join("");
  
  if (myNewColumn == 1 && myNewRow > 2 && value.includes(",")) { 
    // capture first and last name entered
    const firstName = e.value.split(", ")[1]
    const lastName = e.value.split(", ")[0]
  
    // identify the folder where
    // new student folders will exist
    const folder = DriveApp.getFolderById('1l_dYf74ITGiUzu10t72XCig2HWL_Q0PH') 
    
    // create student's new folder
    const newFolder = folder.createFolder(`${lastName}, ${firstName} - Evidence`)
    const newFolderId = newFolder.getId()
    const specificNewFolder = DriveApp.getFolderById(newFolderId)
  
    // add subfolders to student folder
    const ela = newFolder.createFolder("ELA")
    const math = newFolder.createFolder("Math")
    const behavior = newFolder.createFolder("Behavior")
  
    // create form
    const form = FormApp.create(`${lastName}, ${firstName} Feedback Form`);
    form.setTitle(`${lastName}, ${firstName}`)
      .setDescription('Progress on Goal Feedback Form')
  

推荐答案

复制和粘贴值时,事件对象不包含 value .我认为这是您遇到问题的原因.因此,为了同时适用于手动输入和值的复制与粘贴输入,下面的修改如何?

When the value is copied and pasted, the event object doesn't include value. I think that this is the reason of your issue. So in order to work for both manual input and copy&paste input of values, how about the following modification?

if (myNewColumn == 1 && myNewRow > 2 && e.value.includes(",")) {

收件人:

const value = e.range.getValues().flat().join("");  // Added
if (myNewColumn == 1 && myNewRow > 2 && value.includes(",")) {  // Modified

这篇关于粘贴数据时不会触发onEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆