[英]How to log Google Drive File Names to Google Sheets using Google Apps Script
So I am trying to log files (and their Id) from my Drive to a spreadsheet using Google Apps Script.因此,我尝试使用 Google Apps 脚本将我的云端硬盘中的文件(及其 ID)记录到电子表格中。 The problem is that I don't know how to add content to rows of a Google Sheets document.问题是我不知道如何将内容添加到 Google 表格文档的行中。
I know how to log my files and their Id:我知道如何记录我的文件及其 ID:
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
Logger.log(file.getId());
}
Now I would like to log the Name and Id of my files from one folder to a Google sheet.现在我想将我的文件的名称和 ID 从一个文件夹记录到 Google 表格中。 Does anybody know how to achieve this?有谁知道如何实现这一目标?
You'll need to get the spreadsheet, and add the file names to a 2D array.您需要获取电子表格,并将文件名添加到二维数组中。 The following code requires you to enter the spreadsheet file ID and the sheet tab name.以下代码要求您输入电子表格文件 ID 和工作表标签名称。
function logFilesToSheet() {
var arrayForOneRow,file,id,name,outerArray,sh,ss;
id = "";//Enter your spreadsheet file id here
name = "";//Enter the sheet tab name here
ss = SpreadsheetApp.openById(id);
sh = ss.getSheetByName(name);
outerArray = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
file = files.next();
//Logger.log(file.getName());
//Logger.log(file.getId());
arrayForOneRow = [];//Reset on each loop - Each row needs a new array
arrayForOneRow.push(file.getName());
arrayForOneRow.push(file.getId());
outerArray.push(arrayForOneRow);
}
sh.getRange(sh.getLastRow() + 1,1,outerArray.length,2).setValues(outerArray);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.