我的onEdit()函数不断删除我的数据 [英] My onEdit() function keeps deleting my data

查看:70
本文介绍了我的onEdit()函数不断删除我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个标签的Google表格(1)管理员和(2)用户。我有这个脚本:

I have one Google Sheet with two tabs (1) Admin and (2) User. And I've got this script:

function onEdit() {
       var source = SpreadsheetApp.openById('xxx');
       var sourcesheet = source.getSheetByName('Admin');

       var target = SpreadsheetApp.openById('yyy')
       var targetsheet = target.getSheetByName('User');

       var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
       var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();

       targetrange.setValues(rangeValues);
}

因此,基本上,管理员中的任何更新也会反映在用户表中。

So basically, whatever updates in the Admin will also reflect in the User sheet.

问题是,当我更新用户中的某些内容时,除非与管理员中的内容相同,否则我的脚本会自动将其删除。

The problem is, when I update something in the User, my script automatically deletes it unless it's also the same as that in the Admin.

我希望它是单向连接,当用户表更新时,管理表中什么也没有发生。但是当Admin中有某些更新时,用户也会被更新。

I want it to be a one-way connection where when the User sheet updates, nothing happens in the Admin sheet. But when something updates in the Admin, User will also be updated.

非常感谢!

推荐答案

除了@tehhowch的巨大贡献之外,还有另一个解决方案:

Aside from @tehhowch's great contribution, here's another solution:

function onEdit(e) {
       if(e.source.getActiveSheet().getName() !== 'Admin') return;
       e.source.getSheetByName('User').getRange(e.range.getA1Notation()).setValue(e.value);
}

此脚本会将所有更新从管理员复制到用户,但不会复制到其他用户如此一来,只要管理员中的单元格为空或空白,当再次更新管理员时,这还将保留用户表中的所有更改。

This script will copy all updates from the Admin to the User but not the other way around, this will also preserve any changes in the User sheet when the Admin has been updated again provided that the cell is null or blank in the Admin.

这篇关于我的onEdit()函数不断删除我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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