Google App Maker,如何创建复制到剪贴板功能

Google App Maker, how to create copy to clipboard function

我正在尝试使用运行以下脚本的按钮将文本从文本区域复制到剪贴板。 我确实收到复制文本的警报,但是当我尝试将其粘贴到某处时,该值未被复制。

function copmycomment() {
/*get the text area*/
  var copyCo = app.pages.NewPage.children.Panel2.children.tao;

  /*select the text area*/

  copyCo.focus();

    /*copy the value*/

    document.execCommand("copy");

 alert("copied the text:" + copyCo.value);
}

在 Google App Maker 中,TextArea 小部件是由两个 HTML 元素组成的对象; 标签输入。当你执行这一行时:

var copyCo = app.pages.NewPage.children.Panel2.children.tao;

您实际上选择的是一个应用程序制作器对象,而不是包含文本的 HTML 元素;因此,当执行这行代码时:

copyCo.focus();

您没有聚焦要复制的文本,因此 document.execCommand("copy"); 不起作用。

为了实现您的需求,请按照以下步骤操作:

首先,在测试页面中,插入一个 TextArea 小部件,然后在其下方插入一个 Button 小部件。
它应该类似于类似这样的东西:

然后,将以下代码添加到按钮的 onClick 事件处理程序中:

var textField = widget.parent.descendants.TextArea1.getElement().children[1];
textField.select();
document.execCommand('copy');
window.getSelection().removeAllRanges();

请注意:
在行 var textField = widget.parent.descendants.TextArea1.getElement().children[1]; 中,部分 widget.parent.descendants.TextArea1 表示到 TextArea 小部件的路径,因此根据您的操作方式,它可能会有所不同。

应该就这些了。预览您的应用程序,文本应复制到剪贴板。希望对您有所帮助!