简体   繁体   English

如何使用 Google Apps 脚本将 Google Drive 文件名记录到 Google Sheets

[英]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.

相关问题 Google Apps Script 如何链接到 Google Drive 上的 JS 或 CSS 文件 - Google Apps Script How to link to JS or CSS file on Google Drive 如何使用 Google Apps 脚本在 Google Drive 中移动文件夹 - How to move a folder in Google Drive using Google Apps Script 使用 Google Apps 脚本将文件上传到我的 google 驱动器(在 GOOGLE 中没有表格) - Upload file to my google drive with Google Apps Script (NO FORM IN GOOGLE) 使用 Apps Script 检查 Google Drive 中的 zip 文件内容而不解压它? - Check zip file contents in Google Drive using Apps Script not unzipping it? Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 使用Apps脚本将图像上传到Google云端硬盘 - Uploading image to Google Drive using Apps Script 使用 Apps 脚本在 Google 表格中自动编号 - Autonumbering in Google Sheets Using Apps Script 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets 使用Google Apps脚本将文本文件上传到Google云端硬盘 - Upload text file to Google Drive with Google Apps Script 使用 Google Apps 脚本将 Gmail 解析为 Google 表格 - Parsing Gmail into Google Sheets using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM