有没有一种方法可以确定在Google表格脚本中与onEdit一起使用的编辑类型?

时间:2019-05-30 13:06:15

标签: javascript google-apps-script google-sheets gsuite

我需要知道用户是要删除行,编辑单元格还是粘贴一堆单元格。我一直在浏览文档,但是找不到确定用户操作的方法。有没有办法知道onEdit中使用了哪种操作?

3 个答案:

答案 0 :(得分:1)

当前无法使用 Name: spring-boot-postgres-sample-7f7c7479d9-fclwb Namespace: default Priority: 0 PriorityClassName: <none> Node: ip-172-31-11-87/172.31.11.87 Start Time: Thu, 30 May 2019 11:50:24 +0000 Labels: app=spring-boot-postgres-sample pod-template-hash=7f7c7479d9 Annotations: <none> Status: Running IP: 192.168.1.28 Controlled By: ReplicaSet/spring-boot-postgres-sample-7f7c7479d9 Containers: spring-boot-postgres-sample: Container ID: docker://a1278cc26a66eb3b977cf1946d1490c7c5066e9fe0db8c468fd0e8dc47de5ca2 Image: djtijare/a2i-web:v1 Image ID: docker-pullable://djtijare/a2i-web@sha256:cbeac029eb3b65760b904e93ced0b417eca349280ba5a809de713d5fbb2d608f Port: <none> Host Port: <none> Command: /bin/bash -ce tail -f /dev/null State: Running Started: Thu, 30 May 2019 11:50:46 +0000 Ready: True Restart Count: 0 Environment: POSTGRES_USER: <set to the key 'postgres_user' of config map 'postgres-config'> Optional: false POSTGRES_PASSWORD: <set to the key 'postgres_password' of config map 'postgres-config'> Optional: false POSTGRES_HOST: <set to the key 'postgres_host' of config map 'hostname-config'> Optional: false Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-6pksq (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-6pksq: Type: Secret (a volume populated by a Secret) SecretName: default-token-6pksq Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: <none> aquilak8suser@ip-172-31-6-149:~/a2i-web$ kubectl describe pod spring-boot-postgres-sample-7f7c7479d9-fclwb Name: spring-boot-postgres-sample-7f7c7479d9-fclwb Namespace: default Priority: 0 PriorityClassName: <none> Node: ip-172-31-11-87/172.31.11.87 Start Time: Thu, 30 May 2019 11:50:24 +0000 Labels: app=spring-boot-postgres-sample pod-template-hash=7f7c7479d9 Annotations: <none> Status: Running IP: 192.168.1.28 Controlled By: ReplicaSet/spring-boot-postgres-sample-7f7c7479d9 Containers: spring-boot-postgres-sample: Container ID: docker://a1278cc26a66eb3b977cf1946d1490c7c5066e9fe0db8c468fd0e8dc47de5ca2 Image: djtijare/a2i-web:v1 Image ID: docker-pullable://djtijare/a2i-web@sha256:cbeac029eb3b65760b904e93ced0b417eca349280ba5a809de713d5fbb2d608f Port: <none> Host Port: <none> Command: /bin/bash -ce tail -f /dev/null State: Running Started: Thu, 30 May 2019 11:50:46 +0000 Ready: True Restart Count: 0 Environment: POSTGRES_USER: <set to the key 'postgres_user' of config map 'postgres-config'> Optional: false POSTGRES_PASSWORD: <set to the key 'postgres_password' of config map 'postgres-config'> Optional: false POSTGRES_HOST: <set to the key 'postgres_host' of config map 'hostname-config'> Optional: false Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-6pksq (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-6pksq: Type: Secret (a volume populated by a Secret) SecretName: default-token-6pksq Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: <none> 来实现。从Edit Event Object中可以找到的最多信息是该单元格的旧值/新值。 注意:仅当正在编辑一个单元格时,此选项才有效。

例如,您可以使用onEdit()确定已编辑的范围,但不会告诉您已进行的编辑类型。

答案 1 :(得分:1)

您可以为表格使用可安装的更改事件侦听器。

https://developers.google.com/apps-script/guides/triggers/events#change

可以通过测试REMOVE_ROW更改类型来确定是否删除了行。

还可以确定是编辑单个单元格还是多个单元格。参见下面的代码。

粘贴将不会插入行或列,因此,如果发生了更改类型“ EDIT”,则可以假定未插入行或列。我不知道有什么办法可以同时手动编辑多个单元格,因此,如果活动范围包括多个单元格,则可能是“粘贴”。

function nameOfFunction(e) {
  var A1Notation,typeOfChange;

  //Install this function as a trigger for Sheets change

  typeOfChange = e.changeType;//Get the type of change that was made
  Logger.log('typeOfChange: ' + typeOfChange)
  Logger.log('typeof typeOfChange: ' + typeof typeOfChange)

  switch(typeOfChange) {
    case 'REMOVE_ROW':
      Logger.log('A row was deleted')
      break;
    case 'EDIT':
      A1Notation = SpreadsheetApp.getActiveRange().getA1Notation();
      Logger.log('A1Notation: ' + A1Notation)
      if (A1Notation.indexOf(":") === -1) {//There is NOT a colon in the A1 notation
        Logger.log('An Edit was made to a single cell')
      } else {
        Logger.log('An Edit was made to MULTIPLE cells')
      }
      break;
    case 'OTHER':
      /*  This is NOT an edit of types
        EDIT, INSERT_ROW, INSERT_COLUMN, REMOVE_ROW, REMOVE_COLUMN, INSERT_GRID, REMOVE_GRID, FORMAT
      */
      Logger.log('This is NOT an edit or a row Deletion')
      break;
    default:

  }

}

答案 2 :(得分:0)

事件触发器中有什么?

如果您想知道事件对象中包含什么,则可以执行以下操作:

function onEdit(e) {
  Logger.log(JSON.stringify(e));
}

在简单的onEdit()触发器的情况下,它包含的内容是:

[19-05-30 10:19:36:809 MDT] {"authMode":{},"range":{"columnStart":4,"rowStart":4,"rowEnd":4,"columnEnd":4},"source":{},"user":{"nickname":"Your Nickname","email":"Your Email"},"value":"NewValue"}