当我运行我的脚本时没有任何反应

时间:2017-02-28 02:28:23

标签: arrays google-apps-script google-drive-api google-apps

所以我最近发现这个谷歌驱动器脚本并尝试使用它。 该脚本通常应该在我的google驱动器文件夹中获取文件名和URL,并将它们复制到电子表格中。

当我点击“运行”时,我没有收到任何错误,我的驱动器中没有任何反应。

是否应该更改变量以使其有效?

PS:我对编码很陌生,似乎无法找到此代码的错误

提前感谢您的帮助!

以下是代码:

function myFunction() {

  function listFilesInFolder(foldername) {
  // If we have not been provided a foldername, assume we will interact with user.
  var interactive = (typeof foldername === 'undefined');

  // Get name of folder to list
  if (interactive) {
    foldername = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
  }

  if (foldername === '') return;  // No name provided, exit quietly

  var folders = DriveApp.getFoldersByName(foldername);
  if (!folders.hasNext()) {
    if (interactive) Browser.msgBox("Folder not found.");
    return;
  }
  var folder = folders.next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      //"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      niceFileType( file.getMimeType() )
    ];

    sheet.appendRow(data);

  }
}

}

2 个答案:

答案 0 :(得分:1)

两个问题。这需要在附加到电子表格的脚本中运行,并且您具有嵌套函数。

您的代码:

function myFunction() {
  function listFilesInFolder(foldername) {
    ...
  }
 }

删除外部函数decleration及其匹配的右括号。代码将运行。

应该看起来像:

function listFilesInFolder(foldername) {
 ...
}

答案 1 :(得分:0)

请在电子表格中尝试此修改后的代码:

function myfunction(){
//Declaring the function listFolders to temp
var temp = function listFolders(foldername) {
  // If we have not been provided a foldername, assume we will interact with user.
  var interactive = (typeof foldername === 'undefined');

  // Get name of folder to list
  if (interactive) {
    foldername = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
  }

  if (foldername === '') return;  // No name provided, exit quietly

  var folders = DriveApp.getFoldersByName(foldername);
  if (!folders.hasNext()) {
    if (interactive) Browser.msgBox("Folder not found.");
    return;
  }
  var folder = folders.next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      //"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      file.getMimeType() 
    ];

    sheet.appendRow(data);

  }
}
// calls the  function listFolders below
temp()
}