Google Apps - 脚本执行另一个脚本而不调用它的函数

问题描述 投票:0回答:1

Script01.gs

function list_folders_with_no_files_and_subfolders() {
  var folders = DriveApp.getFolders();
  var folderNames = [];
  
  while (folders.hasNext()) {
    var subFolder = folders.next();
    var files = subFolder.getFiles();
    var subFolders = subFolder.getFolders();

    if (files.hasNext()  || subFolders.hasNext()  ) { 
      continue;

    }

      var name = subFolder.getName();
      folderNames.push(name);
    
  }

  
  Logger.log("Log Script 01:");
  folderNames.forEach(function(name) {
    Logger.log(name);
  });
}

Script02.gs

function listAllFolders() {
  var rootFolder = DriveApp.getFolderById('YOUR_FOLDER_ID');
  var folderNamesToCheck = [];
  
  if (rootFolder) {
    var folders = rootFolder.getFolders();
    while (folders.hasNext()) {
      var subFolder = folders.next();
      folderNamesToCheck.push(subFolder);
      listSubFolders(subFolder, folderNamesToCheck);
    }
  }
  
  return folderNamesToCheck;
}



function listSubFolders(folder, folderNamesToCheck) {
  var subFolders = folder.getFolders();
  while (subFolders.hasNext()) {
    var subFolder = subFolders.next();
    folderNamesToCheck.push(subFolder);
    listSubFolders(subFolder, folderNamesToCheck);
  }
}




function listEmptyFolders() {
  var emptyFolders = [];
  var folderNamesToCheck = listAllFolders();
  
  folderNamesToCheck.forEach(function(folder) {
    var subFolders = folder.getFolders();
    var files = folder.getFiles();
    
    if (!files.hasNext() && !subFolders.hasNext()) { 
      emptyFolders.push(folder.getName());
    }
  });
  
  return emptyFolders;
}





var emptyFolders = listEmptyFolders();
Logger.log("Log Script 02:");
Logger.log(emptyFolders);

当我执行 Script01 时,我注意到执行日志在显示 Script01 的日志消息之前显示消息“Log Script 02:”。

但是,当我执行 Script02 时,我只看到消息“Log Script 02:”,这似乎是正确的。

那么 Script01 中的什么触发了 Script02 中的函数呢?所有函数都有唯一的名称,当我在 Script01 中时,我唯一可以选择的函数是 list_folders_with_no_files_and_subfolders,这是正确的。但它仍然会触发 Script02 中的某些内容。

谢谢!

我希望只看到 Script01 的执行日志(list_folders_with_no_files_and_subfolders),而看不到 Script02 的任何内容。

google-apps-script logging execution
1个回答
0
投票

执行 Script01 时,执行日志显示消息“Log Script 02:” 因为

var emptyFolders = listEmptyFolders();
Logger.log("Log Script 02:");
Logger.log(emptyFolders);

处于全局可见性,并且首先 IDE 运行函数

listEmptyFolders()
,然后它就是你的函数
list_folders_with_no_files_and_subfolders()

删除这个

var emptyFolders = listEmptyFolders();
Logger.log("Log Script 02:");
Logger.log(emptyFolders);

来自全球知名度

© www.soinside.com 2019 - 2024. All rights reserved.