问题
The IDs should be aligned like the sample on the left side. But as you can see on the right side, everything is working fine until row 11. Can someone help me with this one?
Here's the code that I'm using. I got it from here.
function myFunction() {
const ss = SpreadsheetApp.getActive();
var SSID = ss.getId();
var spreadsheetFile = DriveApp.getFileById(SSID); //get file by id
//var folderId = spreadsheetFile.getParents().next().getId();
var folderId = "1vn5n00iCpKUWe_JsTFAesQsSmBsXu36i";
const sh = ss.getSheetByName('Sheet4'); // change that to the name of your sheet
const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');
const Folder = DriveApp.getFolderById(folderId);
// 1. Retrieve the file list of all files in `folderId`.
const getFileList = (f, folders = [], fileList = {}) => {
const fs = f.getFiles();
while (fs.hasNext()) {
const file = fs.next()
fileList[file.getName()] = file.getId();
}
const fols = f.getFolders();
const folderObjs = [];
while (fols.hasNext()) folderObjs.push(fols.next());
if (folderObjs.length > 0) {
folders.push(folderObjs);
folderObjs.forEach(fol => getFileList(fol, folders, fileList));
}
return fileList;
};
const fileList = getFileList(Folder);
// 2. Create an array for putting values to Spreadsheet.
const IDs = filenames.map(fn => [fileList[fn] || ""]);
sh.getRange(3,3,IDs.length,1).setValues(IDs);
}
回答1:
I think that the reason of your current issue might be due to by const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');. So how about modifying this as follows?
From:
const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');
To:
const filenames = sh.getRange('B3:B' + sh.getLastRow()).getValues();
来源:https://stackoverflow.com/questions/64940043/google-sheets-working-code-for-get-ids-but-result-should-be-align-on-the-filena