首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google脚本-以编程方式从文件ID访问脚本ID

Google脚本-以编程方式从文件ID访问脚本ID
EN

Stack Overflow用户
提问于 2022-02-14 01:15:48
回答 1查看 415关注 0票数 0

我使用Google脚本制作了许多电子表格的副本,其中嵌入了代码。我希望能够记录每个电子表格副本的脚本ID,而不必手动进入每个电子表格,然后是脚本文件,然后复制脚本ID。我知道,在脚本中,我可以使用ScriptApp.getScriptId(),但这当然不能从独立的脚本中引用文件的副本。

tl;dr:如果我在Google中嵌入了一个脚本,并且我有Google的文件ID,我可以使用Apps脚本编程访问脚本ID吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-22 10:31:04

也许你可以做以下几件事,试一试:

步骤1:您需要在每个脚本中插入以下函数:

代码语言:javascript
运行
复制
function MyScriptID(){return ScriptApp.getScriptId()}

步骤2:使用单独的电子表格操作数据(一种母版表),您将运行以下函数:

代码语言:javascript
运行
复制
function FindScriptID() {


// Insert the ID of the sheet you want to retrieve the script Id from
    var sheet = SpreadsheetApp.openByUrl('your ID')

    // name the sheet where you want to insert the formula

    var destination = sheet.getSheetByName("mysheet");

    // Choose a cell to insert the formula - example: (4,2)

    destination.getRange(4, 2).setFormula('=MyScriptID()');

    SpreadsheetApp.flush();

    // get the displayed ID from the formula, which is actually the ID of the attached script

    var myscriptID = destination.getRange(4, 2).getDisplayValue();

    Logger.log(myscriptID)


// You can now write the ID of your script within the sheet you've ran the above function, choose a sheet where you wan to write the ID - e.g. Sheet1

   var currentsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

//Choose a cell to write the link of you your script or just the ID

currentsheet.getRange(2, 2).setValue('https://script.google.com/a/ais.sch.sa/d/' + myscriptID + '/edit')


}

如果您需要使用许多电子表格来迭代,那么现在可以改进这个母版表,尽管所有这些都可以一次完成:

在总表内:

  • 您列出了所有电子表格(Urls)
  • ,如前面提到的,在它们的所有脚本(function MyScriptID(){return ScriptApp.getScriptId()})顶部,您迭代了所有电子表格链接以插入公式
  • ,您读取显示的值H 211H 112您将检索到的脚本in或链接H 213F 214写入主表

有时,显示的值会出现一些错误,如('Loading...')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71106234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档