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

Google Apps Script - Programmatically Access Script ID From File ID

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

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

也许你可以这样做,试试看:

第 1 步:您需要在每个脚本的顶部插入以下函数:

function MyScriptID(){return ScriptApp.getScriptId()}

第2步:使用单独的spreadsheet操作数据(一种高手sheet),你会运行以下函数:

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')


}

如果您有许多传播sheet要使用以便一次迭代所有传播,您现在可以改进这个大师sheet:

主内sheet:

  • 您列出了所有传播sheets (urls)
  • 如前所述,您将他们的所有脚本 (function MyScriptID(){return ScriptApp.getScriptId()}) 都包含在顶部
  • 您遍历所有传播sheet 链接以插入公式
  • 您阅读了显示的值
  • 您将检索到的脚本 ID 或链接写入主 sheet

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