Google Apps script move My drive Files to Team drive folder based on file name

孤街浪徒 提交于 2021-01-29 07:16:41

问题


I am new to Google Apps Script. I'm trying to get for our workplace a script to move files from a single folder on a Google My Drive, over to a Team Drive if part of the filename has a certain phrase. However the script is still moving any and all files from the My Drive source folder to the Team Drive target folder, even those that don't have the "required" phrase in the file name. Example file names used:

ALT ADJ 01_03_2018

ALT CHG 01_03_2018

ALT CHG 01_04_2018

ALT PMT 01_03_2018

APX ADJ 01_03_2018

Enabled Drive API v2 in Advanced Google Services under Resources

function moveFileToFolder() { 
  var upldFldr=DriveApp.getFolderById('<<original Folder ID>>');
  var files=upldFldr.getFiles();
  supportsTeamDrives: true;
  while(files.hasNext()) {
    var file=files.next();
    var key=file.getName().slice(0,7);  //intended to take the first 7 characters of the filename.
      if (key = "ALT CHG") {
        supportsTeamDrives: true;
        supportTeamDrives: true;
        var targetFolder = DriveApp.getFolderById('<<new folder ID>>');
        targetFolder.addFile(file);
      };
  }
}

In the code, I'm trying to get only files that have the first 7 letters of ALT CHG in the file name to be moved/copied to the Team Drive folder. Instead, all of the files end up in the Team Drive Folder. Note this is the entire code start to finish.


回答1:


Change:

if (key = "ALT CHG") {

to

if (key == "ALT CHG") {

This should fix the issue, i.e. use == instead of =



来源:https://stackoverflow.com/questions/56385933/google-apps-script-move-my-drive-files-to-team-drive-folder-based-on-file-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!