如何使用Google脚本替换文字?

时间:2017-07-28 17:57:10

标签: image google-apps-script google-sheets

我想在Google电子表格中显示来自Google云端硬盘的图片。 我必须替换文字:          open? 对于:          uc?export=download& 在可共享链接中:          https://drive.google.com/open?id=0BwJUnx7uETDmaG42eGdEVUVocU0

我尝试过这个解决方案https://stackoverflow.com/a/42819032/2661411

” 我制作了一个两行脚本来使用Google云端硬盘中图片的共享链接。

  1. 转到工具>脚本编辑器。
  2. 复制,粘贴以下代码
  3. 点击“运行以获取权限”

    function DRIVE_IMAGE(link){ prefix_url = "https://docs.google.com/uc?export=download&"; link.replace("open?", "uc?export=download&"); }

  4. 使用脚本:

    1. 在Google云端硬盘中复制图片的分享链接。
    2. 转到单元格
    3. 输入公式=IMAGE(DRIVE_IMAGE("COPIED_LINK"))
    4. 但对我不起作用,它说:

      TypeError: Cannot call method "replace" of undefined. (line 3, file "ImgView")

      有什么想法吗?我希望它能保持简单。

1 个答案:

答案 0 :(得分:2)

您需要使用https://docs.google.com/uc?export=download&id=<<IMAGE ID>>。请参考以下公式。

=image("https://docs.google.com/uc?export=download&id=0BwJUnx7uETDmaG42eGdEVUVocU0")

<强>脚本

/**
 * @customfunction
 */
function DRIVE_IMAGE(link){
   var newLink = link.replace("https://drive.google.com/open?", "https://docs.google.com/uc?export=download&");
   return newLink;
}

现在,您可以使用公式=IMAGE(DRIVE_IMAGE("https://drive.google.com/open?id=0BwJUnx7uETDmaG42eGdEVUVocU0"))