【发布时间】:2019-08-28 13:07:58
【问题描述】:
我需要每天一次(通过触发器)将数据从不受保护的单元格从表 1 复制到表 2。然后保护复制的单元格。第二天只复制新的未受保护的单元格和数据,然后也保护它们。
我的问题是 - 我无法获得正确的复制到 Table2 的范围。 请给点建议
附:对不起我的英语
表 1 - https://docs.google.com/spreadsheets/d/1fNshucMoC9wFjGKl9ZLvk2jw9yI2llKQ9dioGbO-F0s/edit?usp=sharing
表 2 - https://docs.google.com/spreadsheets/d/11AvUWe5vu6vHNXCPfXlK1Es3JLqQh13Fy_22l-obwP0/edit?usp=sharing
function protect_sales()
{
// Connect to Table 1
var sheetIncome = SpreadsheetApp.getActive().getSheetByName('Sales');
var lastRow = sheetIncome.getLastRow();
var protection = sheetIncome.protect();
var unprotected = protection.getUnprotectedRanges();
// Connect to Table 2
var sss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/11AvUWe5vu6vHNXCPfXlK1Es3JLqQh13Fy_22l-obwP0/edit');
var ss = sss.getSheetByName('Sales');
var lastSRow = ss.getLastRow();
var target_range = ss.getRange(lastSRow+1,1,lastRow,5);
ss.insertRowAfter(lastSRow);
// Copy Data to Table 2
ss.getRange(2,1).setValues(unprotected);
// Protect our Range
var dataRange = sheetIncome.getRange(1, 1, lastRow, 5)
var protection = dataRange.protect().setDescription('protected range');
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
【问题讨论】:
-
我想你可能对如何使用
getUnprotectedRanges()有误解。所以我无法理解你的目标。关于ss.getRange(2,1).setValues(unprotected),在这种情况下,你想要unprotected的值是多少? -
你是对的。我不明白如何使用“getUnprotectedRanges()”。我需要新添加的和未受保护的行的日常副本(到 Table2),然后保护它们。
标签: google-apps-script google-sheets