在特定时间锁定 Google 表格中的单元格

本文介绍了在特定时间锁定 Google 表格中的单元格的处理方法,对大家解决问题具有一定的参考价值

问题描述

我想对我的 Google 表格单元格进行编码,使某些单元格每天在特定时间自动锁定.我应该可以编辑它,但不能编辑我的合作者.

我该如何解决这个问题?

在示例表中(此处链接),您将看到某些日期(15/7/2015-17/7/2015).我想对其进行编码,以便每天晚上 10 点锁定每个日期(例如 A1:B6).

解决方案

As @NightShadeQueen 建议 时间驱动的触发器protect()-可以使用 Class Range 的方法.

  1. 在工作表中打开脚本编辑器(工具 > 脚本编辑器...)
  2. 创建锁定范围的代码,可能如下所示:

<小时>

如果您的工作表看起来不像示例工作表,您将不得不修改代码.特别是 lockRanges() 函数,它定义了哪些范围应该受到保护.目前,它从单元格 A1 开始,一次向下 7 行,直到到达工作表的末尾.

var ss = SpreadsheetApp.getActiveSpreadsheet();var sheet = ss.getSheetByName('Sheet1');//在此处插入工作表名称功能锁范围(){//第一个要锁定的单元格var 行 = 1;var col = 1;//获取工作表中包含数据的最后一行var lastRow = sheet.getLastRow();//循环直到通过sheet中的最后一行while(row <= lastRow){//锁定当前单元格锁定范围(行,列);//从当前(下一个日期)转到第 7 行行 = 行 + 7;}}功能锁范围(行,列){var range = sheet.getRange(row, col);//创建保护对象.设置描述,任何你喜欢的.var protection = range.protect().setDescription('Protected, row ' + row);//在删除其他人之前,确保当前用户是编辑者.否则,如果用户的编辑//权限来自组,删除组时脚本将抛出异常.var me = Session.getEffectiveUser();protection.addEditor(me);protection.removeEditors(protection.getEditors());如果(保护.canDomainEdit()){protection.setDomainEdit(false);}}

  1. 添加时间驱动触发器

    1. 仍在脚本编辑器中;转到资源 > 当前项目的触发器.
    2. 创建一个新的触发器
    3. 选择Run = lockRanges
    4. 选择 Events = Time-drivenDay timer晚上 10 点到晚上 11 点(或任何你想要的时间)

<块引用>

注意:时间可能略有随机(阅读更多)

<小时>

就是这样,现在范围将在您选择的时间受到保护.与您手动保护的范围一样,它们显示在数据 > 受保护的工作表和范围中.

有什么不清楚的吗?问吧!

I want to code my Google Sheets cells in a way that certain cells automatically lock at a specific time every day. I should be able to edit it, but not my collaborators.

How do I pull this off?

In the sample sheet (link here), you will see certain dates (15/7/2015-17/7/2015). I want to code it so that each date (eg A1:B6) is locked daily, 10 pm.

解决方案

As @NightShadeQueen suggested a combination of Time-driven triggers and the protect()-method of the Class Range can be used.

  1. Open the script editor in the sheet (Tools > Script editor...)
  2. Create code that locks the ranges, might look something like this:


You'll have to modify the code if your sheet doesn't look like the Sample sheet. Especially the lockRanges()-function, which defines which ranges should be protected. Currently, it starts with cell A1, and steps down 7 rows at a time until the end of the sheet is reached.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); //INSERT SHEET NAME HERE

function lockRanges() {
  //First cell to lock
  var row = 1;
  var col = 1;

  // Get last row with data in sheet
  var lastRow = sheet.getLastRow();

  //Loop until last row in sheet is passed
  while(row <= lastRow){
    //Lock current cell
    lockRange(row, col);

    // Go to row 7 steps down from current (the next date)
    row = row + 7;
  }
}



function lockRange(row, col){
  var range = sheet.getRange(row, col);

  // Create protection object. Set description, anything you like.
  var protection = range.protect().setDescription('Protected, row ' + row);

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
}

  1. Add the Time-driven trigger

    1. Still in the Script editor; go to Resources > Current project's triggers.
    2. Create a new trigger
    3. Choose Run = lockRanges
    4. Choose Events = Time-driven, Day timer, 10pm to 11pm (or whenever you want)

NOTE: The time may be slightly randomized (read more)


That's it, now the ranges will be protected at the time you chose. As with ranges you protect manually, they show up in Data > Protected sheets and ranges.

Something unclear? Ask away!

这篇关于在特定时间锁定 Google 表格中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1162

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1065

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:802

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:892

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:927

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:874

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:858

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:828

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1707

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1303