簡體   English   中英

我可以保護單元格(不允許用戶編輯)並運行在這些單元格中進行更改的腳本(例如隱藏行、取消隱藏行)嗎?

[英]Can I protect cells (user not allowed to edit) and run script that makes changes in these cells (e.g. hide row, unhides row)?

是否可以保護用戶的某些單元格,同時讓用戶有機會運行在單元格中進行更改的腳本,例如復制、隱藏、取消隱藏單元格? 這樣,單元格可以由腳本更改,但不能由用戶自己更改。

我在工具 -> 腳本編輯器中編寫腳本。 這樣,腳本始終從用戶的帳戶運行。 也許解決方案是從管理員帳戶運行腳本? 是否可以? 因為管理員可以訪問所有受保護的單元格。

根據 Amolina Web Apps 評論中的幫助可能會有所幫助,但我仍然無法解決我的問題。 我瀏覽了一些關於 Web Apps 的材料,但通常這些材料用於在 html 網站和 google 表格之間發送數據,而不是作為從管理員級別運行某些腳本的工具。 我被卡住了:(

在我的工作表中,它看起來像這樣:

  • 我創建了一張供用戶操作的工作表

  • 所有腳本都由工作表上創建的按鈕觸發

  • 正在使用的每一行都有一些受用戶保護的單元格

  • 將來我想復制工作表並將其發送給不同的用戶進行處理,因此在復制工作表后解決方案也能工作會很好; 腳本和工作流不會改變,只有用戶將填寫的數據

我使用的腳本示例:

1)

function copyRow() {
var sheet = SpreadsheetApp.getActiveSheet(); 
var currentRow = sheet.getCurrentCell().getRow();  

sheet.insertRowsBefore(currentRow, 2);
var rangeToCopy = sheet.getRange(currentRow + 2, 1, 2, sheet.getMaxRows());
rangeToCopy.copyTo(sheet.getRange(currentRow, 1));

sheet.getRange(currentRow, 2,1,6).clearContent();
sheet.getRange(currentRow, 12,1,sheet.getMaxColumns()).clearContent();
sheet.getRange(currentRow,50).setValue("d");

sheet.getRange(currentRow, 2,1,6).setBackground("white");
sheet.getRange(currentRow + 1, 12,1,26).setBackground("white");

var protection = sheet.getRange(currentRow, 1,1,1).protect();
protection.addEditor("adminMail@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}

var protection = sheet.getRange(currentRow, 8,1,sheet.getMaxColumns()).protect();
protection.addEditor("adminMail@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);}}

2)

function deleteRow()
var sheet = SpreadsheetApp.getActiveSheet();
var selection = sheet.getSelection();
var currentCell = selection.getCurrentCell().getRow();

var cell2 = sheet.getRange(currentCell,50);
var cellBefore = sheet.getRange(currentCell - 1, 50);
var cellAfter = sheet.getRange(currentCell + 1, 50);

if(cell2.getValue() == "r" && (cellBefore.getValue() != "r" && cellAfter.getValue() != "r") == false){

var ui = SpreadsheetApp.getUi();
var response = ui.alert('Napewno chcesz usunąć pozycję?', ui.ButtonSet.YES_NO);

if (response == ui.Button.YES) {

  sheet.deleteRow(currentCell);
 }
}
else{
Browser.msgBox('Nie można usunąć wiersza', Browser.Buttons.OK); 
}
}

感謝@Amolina,我能夠解決這個問題。 謝謝!!!

我使用 WebApp 來允許用戶以管理員身份運行腳本。

Google 腳本中的代碼 inCode 示例:

function copyRow(){

var sheet = SpreadsheetApp.getActiveSheet();

var currentRow = sheet.getCurrentCell().getRow();

//url web app

var urlScript = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var sheetName = SpreadsheetApp.getActiveSheet().getName();

var type = "addRow";

var url = "https://script.google.com/macros/s/AKfycbwC7og_____1Bf0WP8v5tQ/dev";

var urlParam = (url+"?urlScript="+urlScript+"&sheetName="+sheetName+"&currentRow="+currentRow
              +"&type="+type);  

//przesłanie do webapp
var html = "<script>window.open('" + urlParam + "');google.script.host.close();  </script>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, "skrypt w trakcie działania...");
}

WebApp 中的腳本

function doGet(e) {

//parametry
var urlScript = e.parameter.urlScript;
var sheetName = e.parameter.sheetName;
var currentRow = Number(e.parameter.currentRow);
var type = e.parameter.type;

//odniesienie do mojego sheet

var file = SpreadsheetApp.openByUrl(urlScript);
var sheet = file.getSheetByName(sheetName);

//funkcje właściwe
if(type == "addRow"){
//kod właściwy
sheet.insertRowsBefore(currentRow, 2);
var rangeToCopy = sheet.getRange(currentRow + 2, 1, 2, sheet.getMaxRows());
rangeToCopy.copyTo(sheet.getRange(currentRow, 1));

sheet.getRange(currentRow, 2,1,6).clearContent();
sheet.getRange(currentRow, 12,1,sheet.getMaxColumns()).clearContent();
sheet.getRange(currentRow,50).setValue("d");

sheet.getRange(currentRow, 2,1,6).setBackground("white");
sheet.getRange(currentRow + 1, 12,1,26).setBackground("white");

var protection = sheet.getRange(currentRow, 1,1,1).protect();
protection.addEditor("adminMail@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}

var protection = sheet.getRange(currentRow, 8,1,sheet.getMaxColumns()).protect();
protection.addEditor("adminMail@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM