【问题标题】:Prevent manual copy of spreadsheet Google script防止手动复制电子表格 Google 脚本
【发布时间】:2017-04-18 22:26:28
【问题描述】:

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

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

我曾想过在单元格中设置一个标志,但当然这只适用于第一次,因为手动副本也会复制标志。如果我可以检测到文件何时被手动复制,我可以重置标志,然后在打开时对其进行测试。

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

非常感谢

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    您可以解决这个特定问题:

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

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

    但是,如果您复制文档,则不会复制该属性。您可以在 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")
     }
    }
    

    希望有帮助!

    【讨论】:

    • 我实现了类似的东西,其中文档 ID 是我存储在“合法”副本上的内容,但正如这个答案所说,任何东西都足够了。如果 Google 将来更改复制以包括文档属性,那么像我的 per-doc 特定解决方案之类的东西将成为必要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多