Arkusze Google: działający kod identyfikatorów pobierania, ale wynik powinien być wyrównany w wierszu nazw plików

Nov 20 2020

Identyfikatory powinny być wyrównane jak próbka po lewej stronie. Ale jak widać po prawej stronie, wszystko działa dobrze do wiersza 11. Czy ktoś może mi w tym pomóc?

Oto kod, którego używam. Mam to stąd .

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);
}

Odpowiedzi

2 Tanaike Nov 21 2020 at 05:36

Myślę, że przyczyna twojego obecnego problemu może być spowodowana przez const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');. A co z modyfikacją tego w następujący sposób?

Od:

const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');

Do:

const filenames = sh.getRange('B3:B' + sh.getLastRow()).getValues();