简体   繁体   English

如何使用应用程序脚本谷歌驱动器从谷歌表中的父文件夹中获取所有子文件夹名称

[英]How to get list all sub folders name from parent folders in google sheet with apps script google drive

How can i list all sub folders name from google drive into google sheets?如何将谷歌驱动器中的所有子文件夹名称列出到谷歌表格中?

Here is the code, it's working but I cant put them in to google sheets.这是代码,它正在工作,但我无法将它们放入谷歌表格。

enter image description here In the place of logger i need replace the google sheet.在此处输入图像描述在记录器的位置我需要替换谷歌表。 Please help me out in this scenario?请在这种情况下帮助我?

If you are planning to use custom function to write the list of sub-folder names in Google Sheets, It will not work because your function require authorization when using DriveApp it will throw an error message You do not have permission to call X service.如果您打算使用自定义 function在 Google Sheets 中写入子文件夹名称列表,它将不起作用,因为您的 function 在使用 DriveApp 时需要授权,它会抛出错误消息You do not have permission to call X service. , the service requires user authorization and thus cannot be used in a custom function as stated here . ,该服务需要用户授权,因此不能在此处所述的自定义 function 中使用。

You can write your sub-folder names in Google Sheets usingSpreadsheet Service .您可以使用电子表格服务在 Google 表格中写入您的子文件夹名称。 But you need to specify the specific sheet and the cell where you want to write your data.但是您需要指定要写入数据的特定工作表和单元格。

Sample Code:示例代码:

function findSubFolder(){
  var childFolder = DriveApp.getFolderById('xxxxxxxx').getFolders();
  var activeSheet = SpreadsheetApp.getActiveSheet();
  var activeCell = activeSheet.getActiveCell();

  var childNames = [];
  while (childFolder.hasNext()) {
    var child = childFolder.next();
    Logger.log(child.getName());
    childNames.push([child.getName()]);
  }
  
 
 activeSheet.getRange(activeCell.getRow(),activeCell.getColumn(),childNames.length,1).setValues(childNames);
}

What it does?它能做什么?

  1. Get your active sheet using SpreadsheetApp.getActiveSheet() and the active cell using Sheet.getActiveCell()使用SpreadsheetApp.getActiveSheet()获取活动工作表,使用Sheet.getActiveCell()获取活动单元格
  2. Get all your sub-folder names and add that to childNames array using Array.prototype.push()使用Array.prototype.push()获取所有子文件夹名称并将其添加到childNames数组

Notice that when I pushed the child names I used [sub-folder name] , This is needed when we want use Range.setValues() which accepts a 2-d array where I want to have multiple rows.请注意,当我推送子名称时,我使用了[sub-folder name] ,当我们想要使用 Range.setValues() 时需要这样做,它接受我想要多行的二维数组。

  1. Write the childNames array to the active sheet starting from the active cell.从活动单元格开始将childNames数组写入活动工作表。 Using Sheet.getRange(row, column, numRows, numColumns) and Range.setValues(values)使用Sheet.getRange(row, column, numRows, numColumns)Range.setValues(values)

Output: Output:

在此处输入图像描述


If you want to write the sub-folder names on a single cell(in your active cell), you can replace如果您想在单个单元格(在您的活动单元格中)上写子文件夹名称,您可以替换

activeSheet.getRange(activeCell.getRow(),activeCell.getColumn(),childNames.length,1).setValues(childNames);

to

activeCell.setValue(childNames.flat().toString());

Output: Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从子文件夹(在 Google Drive 上)获取特定文件(Google Sheet),然后更改单元格的值 - Get specific files (Google Sheet) from sub-folders (on Google Drive) and then change a value of a cell 使用 Google Apps 脚本从 google drive 文件夹中获取 Google 表格中列出的文件夹和文件 - Get folders and files listed in Google Sheets from a google drive folder using a Google Apps script 如何使用 Google Script 从多个子文件夹在 Google Drive 文件夹中一起搜索特定文件名和文件类型 - How to search a Specific File Name and File Type together in Google Drive folder from multiple sub folders using Google Script 为什么这个脚本没有得到谷歌驱动器中的所有文件夹? - Why this script not getting all the folders in google drive? 将谷歌驱动器中的文件和文件夹列表导入谷歌表 - Import list of files and folders in google drive into google sheet 在Google Apps脚本中,我将如何获取所有子文件夹,所有子子文件夹以及所有子子文件夹等? - In Google Apps Script, how would I get all subfolders and all subsubfolders and all subsubsub folders etc.? 获取 Gsuite google drive 上的文件夹列表,在 Google Sheet 上按字母顺序列出它们 - Get list of folders on Gsuite google drive, list them alphabetically on a Google Sheet 如何使用Google Apps脚本将文件夹从MyDrive复制到TeamDrive? - How to copy folders from MyDrive to a TeamDrive with Google Apps Script? 列出谷歌表格脚本中的所有文件夹和子文件夹 - List all folders and subfolders in a google sheets script 带有多个子文件夹的 Google 云端硬盘文件夹 - Google drive folder with multiple sub folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM