防止手动复制电子表格Google脚本

时间:2017-04-18 22:26:28

标签: google-apps-script

我有一张每月创建的Google表格,因此写了一个脚本,该脚本将在当前月份的工作表中复制特定文件夹,然后使用旧数据填充新电子表格中的单元格。它运作良好。

我正在尝试提出一个解决方案来警告用户,如果他们尝试使用“制作副本”菜单项手动复制电子表格,而不是使用提供的自定义菜单项。

我考虑过在单元格中设置一个标志,但当然这只是第一次有效,因为手动副本也会复制标志。如果我能够检测到手动复制文件的时间,我可以重置标志,然后在onOpen上测试它。

有人可以指出我正确的方向。不幸的是,用户需要能够编辑文档,所以我认为我不能在文件菜单中禁用该选项。

非常感谢

1 个答案:

答案 0 :(得分:2)

您可以解决此特定问题:

  

我想过在单元格中设置一个标志,但当然只是   第一次工作,因为手动副本也复制标志。

通过使用PropertiesService,您可以使用document属性将属性添加到特定文档。任何用户在该特定文档上运行的脚本都可以访问此属性。

但是,如果复制文档,则不会复制该属性。您可以在onOpen期间查找此属性,并确定文档是否按照您希望的方式进行复制。

您可以这样做:

function setProp(){               //use this to set the property to the document
 var docProp =  PropertiesService.getDocumentProperties(); 
  docProp.setProperty("Copied", "false")              //Properties are stored as text immaterially of the type of value you pass to it!
}

function getProp(){               //use this to check if the property of the document was set, if set will return true, else false
  var docProp =  PropertiesService.getDocumentProperties(); 
  Logger.log(docProp.getProperty("Copied"))
  return docProp.getProperty("Copied") == "false"

}

function onOpen(e){                      
 var ui = SpreadsheetApp.getUi()
 if(getProp()){                        // Check to see if the property was set for the sheet. 
  ui.alert("Copied correctly")
 } else {                              // This will trigger each time they open the sheet,
   ui.alert("Copied Incorrectly, please use custom function to make a copy")
 }
}

希望有所帮助!