[英]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.
但是您需要指定要写入数据的特定工作表和单元格。
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);
}
childNames
array using 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() 时需要这样做,它接受我想要多行的二维数组。
childNames
array to the active sheet starting from the active cell.childNames
数组写入活动工作表。 Using Sheet.getRange(row, column, numRows, numColumns) and Range.setValues(values) 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.