获取文件的所有ID [英] Get all the ID of the files

查看:65
本文介绍了获取文件的所有ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按年级使用相同内容的文件夹.我使用了IMPORT RANGE函数来连接电子表格.因此,每次创建另一个年级时,我都需要更新ID.我可以问一下,是否有可能无需编辑或一一输入就可以获取正在处理的所有文件的所有ID?也许可以简化脚本?

I'm working on a folder per grade level with the same content. I used the IMPORT RANGE function to connect the spreadsheets. So I need to update the IDs every time I create another grade level. May I ask if it is possible for me to get all the IDs of all the files that I'm working on without editing or typing them one by one? Maybe a script that can make it easier?

这是ID列表的图片.

如果可能的话,我正在考虑的流程是获取文件的文件名,然后获取其ID.

If it is possible, the flow I’m thinking of is to get the filename of the files and then get the id of it.

推荐答案

说明:

  • 以下脚本将获取在 B3:B 范围内的所有文件名.然后,我们使用的 forEach 文件名 getFilesByName 以获取与该名称对应的文件.确保您没有相同名称的文件,否则此过程将返回相同名称的多个ID,因此逻辑将无法正常工作.

    Explanation:

    • The following script will get all the file names in the range B3:B. Then forEach file name we use getFilesByName to get the files corresponding to that name. Make sure you don't have files with the same name, otherwise this process will return multiple IDs for the same name and therefore the logic won't work.

      最后,我们使用 getId 来获取每个文件的ID并将其存储到数组中.然后,按照您在屏幕快照中的指示,将此数组从单元格 C3 粘贴到C列.

      Finally we use getId to get the ID of each file and store it into an array. This array will be then pasted to column C starting from cell C3 as you indicate in the screenshot.

      function myFunction() {
        const ss = SpreadsheetApp.getActive();
        const sh = ss.getSheetByName('Sheet1'); // change that to the name of your sheet
        const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');
        const IDs = [];
        const folderId = "put your folder id here";
        const Folder=DriveApp.getFolderById(folderId);
        filenames.forEach(fn=>{
            let Files = Folder.getFilesByName(fn);
            while(Files.hasNext()){
                let file = Files.next();
                IDs.push([file.getId()]);
            }
        });
        sh.getRange(3,3,IDs.length,1).setValues(IDs);
      }
      

      这篇关于获取文件的所有ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆