【问题标题】:Is there a way to add protection to cells that are being copy pasted in Google Sheets?有没有办法为正在复制粘贴到 Google 表格中的单元格添加保护?
【发布时间】:2021-03-26 12:22:50
【问题描述】:

我对 Google 表格保护有疑问。除了某些单元格,我正在使用保护整张纸。

假设我有 3 个范围在 6 行和 6 列中受到保护。

(A23:B26, E24:E25, E27:E28)

所以,我有一个工作宏,用户可以在其中选择一个单元格并运行该宏。 该宏成功地从顶部复制 6 行并将它们粘贴到下方。

请看下面的函数AddRow()

当这 6 行被复制时,我希望对被复制的行和生成的新行进行保护。 但是,Google 表格不会自动执行此操作。

那么,我该怎么做才能将新范围添加到工作表的保护中。

例如:新范围将是原始范围加上复制的单元格。

{(A23:B26, E24:E25, E27:E28), (A30:B33, E31:E32, E34:E5)}

据我了解,这应该不是问题,但对我不起作用。

AddRow() 函数是一个工作宏,用户在其中选择一个单元格并运行该宏。 该宏成功地从顶部复制 6 行并将它们粘贴到选定的单元格中。

function AddRow() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();
  if (spreadsheet.getCurrentCell().getValue() == "AddRow")
  {
    sheet.getRange(spreadsheet.getCurrentCell().getRow() - 7, 1, 6, sheet.getMaxColumns()).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    spreadsheet.getCurrentCell().offset(7, 0).activate();
    spreadsheet.getCurrentCell().setValue("AddRow");
  }
};

我已经看到并尝试了保护宏,但是,我不确定如何获得这些范围以及由于该宏可以运行 100 次,我希望它能够在除这些范围之外的整个工作表上保持保护在这 600 行中。

我目前拥有的保护码:

      var spreadsheet = SpreadsheetApp.getActive();
  sheet = spreadsheet.getActiveSheet();
  var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET);
  var protection = allProtections[0];
  protection.setUnprotectedRanges([spreadsheet.getCurrentCell().offset(-14, 0, 8, 6), spreadsheet.getCurrentCell().offset(-5, 0, 6, 6)]);
  protection.setUnprotectedRanges([spreadsheet.getCurrentCell().offset(20, 0, 8, 6), spreadsheet.getCurrentCell().offset(29, 0, 6, 6), spreadsheet.getCurrentCell().offset(36, 0, 6, 6)]);
};

这是手动设置保护,即使那样它也不起作用。

基本上,我希望我的宏复制行,然后转到工作表保护,添加新的单元格范围(并保留当前受保护的范围)并保存保护。

还有一些可以在支持页面上使用的东西。

  var ranges = p.getUnprotectedRanges();
  var newRanges = [];
  for (var i = 0; i < ranges.length; i++) {
    newRanges.push(sheet2.getRange(ranges[i].getA1Notation()));
  } 
  p2.setUnprotectedRanges(newRanges);

此代码将保护复制到另一张纸上。 就我而言,我希望它发生在同一张纸上但在不同的行中。

任何帮助将不胜感激。

https://docs.google.com/spreadsheets/d/1zS47CTanrhaYerzzxeBqSGozcM4YZPPyre1kt8YyEBQ/edit?usp=sharing

我在这里以更好的方式解释了这个问题。

【问题讨论】:

  • 所以,如果我正确理解了您的问题,您有一个要复制到的受保护工作表,并希望允许任何人编辑复制的单元格。是对的吗?你能分享一个简单的例子来看看你想要什么吗? (我会制作一个副本以供使用,查看编辑就足够了)
  • 好的,会的,谢谢。
  • @Marti 谢谢你的帮助。我已经在工作表中更好地解释了它。您拥有编辑权限。 docs.google.com/spreadsheets/d/…

标签: javascript google-sheets


【解决方案1】:

以下代码似乎对我有用,即使我不能 100% 确定它是否是您想要的:

function addPeriod() {
  const spreadsheet = SpreadsheetApp.getActive()
  const sheet = spreadsheet.getActiveSheet()
  const currentCell = spreadsheet.getCurrentCell()

  if (currentCell.getValue() === 'AddRow') {
    // Copy values
    const copySource = sheet.getRange(currentCell.getRow() - 6, 1, 6, sheet.getMaxColumns())
    copySource.copyTo(currentCell, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false)

    // Set protections exceptions
    const protections = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)
    if (protections.length === 1) {
      const protection = protections[0]
      const exceptions = protection.getUnprotectedRanges()
      const unprotectRange = currentCell.offset(1, 0, 4, 1)
      protection.setUnprotectedRanges([...exceptions, unprotectRange])
    }


    // Prepare to be able to add next period
    const nextCell = currentCell.offset(6, 0)
    nextCell.setValue('AddRow')
    nextCell.activate()
  }
}

此代码添加了一个新句点并从彩色范围中删除了保护。

它与您所拥有的类似,但请注意 setUnprotectedRanges(ranges) 是应不受保护的所有范围的列表。因此,您需要获取当前列表并在末尾添加您的列表。我使用扩展语法 ([...exceptions, unprotectRange]) 来做到这一点。

参考文献

【讨论】:

  • 谢谢,这正是我想要的。
猜你喜欢
  • 2023-03-23
  • 2023-03-17
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2019-06-04
  • 1970-01-01
相关资源
最近更新 更多