gpt4 book ai didi

javascript - getFiles 和 getFolders 总是只返回一个文件/文件夹

转载 作者:行者123 更新时间:2023-11-30 19:54:59 27 4
gpt4 key购买 nike

当尝试从我的 Google 云端硬盘接收所有文件和文件夹时,函数 .getFiles() 仅返回一个文件,而 .getFolders() 仅返回一个文件夹. 我的驱动器中确实有多个文件夹和文件。

我的代码:

function run() {
try {
const folders = DriveApp.getFolders();
const files = DriveApp.getFiles();

while (folders.hasNext()) {
const folder = folders.next();
console.log(folder.getName())


folder.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT)
if (folder.getSharingAccess() !== DriveApp.Access.PRIVATE)
console.error("Permission was not changed, or changed incorrectly")
}

while (files.hasNext()) {
const file = files.next();
console.log(file.getName())
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT)
if (file.getSharingAccess() !== DriveApp.Access.PRIVATE)
console.error("Permission was not changed, or changed incorrectly")
}
} catch (err) {
console.error(err)
}
}

我希望 console.log(file.getName())console.log(folder.getName()) 显示不同文件的信息,但是它总是相同的:

Jan 8, 2019, 4:26:05 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:05 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:06 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:06 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:07 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:07 PM Debug TestFolder1NestedFolder1
Jan 8, 2019, 4:26:10 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:10 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:10 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:11 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:11 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:11 PM Debug Change Sharing to PRIVATE
Jan 8, 2019, 4:26:11 PM Debug Change Sharing to PRIVATE

最佳答案

在这种情况下使用 const 键名进行赋值会阻止您获得所需的结果。

JavaScript const - Documentation

The value of a constant cannot change through reassignment, and it can't be redeclared

目前:

const folder = folders.next();

更改为:

var folder = folders.next();

关于javascript - getFiles 和 getFolders 总是只返回一个文件/文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54094892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com