firekid2018
firekid2018

Reputation: 145

How can we run multiple function in single google drive app?

I want to run two job in google drive app script in one single run, can we do that? sorry i am noob, i don't have much knowledge about php or scripting. here is sample code

function copyfile(e) {
  var filenames = DriveApp.getFoldersByName("abc").next().getFilesByName("filename.txt").next().getBlob().getDataAsString().split("\r\n").filter(String);
  var sourceFolderName = "music";
  var destinationFolderId = "folder id 1";
  var source_folder = DriveApp.getFoldersByName(sourceFolderName).next();
  for (var i = 0; i < filenames.length; i++) {
    var filename = filenames[i];
    var file = source_folder.getFilesByName(filename);
    if (file.hasNext()) {
var dest_folder = DriveApp.getFolderById(destinationFolderId);
      var srcFile = file.next();
      var newName = srcFile.getName();
      srcFile.makeCopy(newName, dest_folder);
    }
  }
   copyfile1(e); // Added
}

function copyfile1(e) {
  var filenames = DriveApp.getFoldersByName("abc").next().getFilesByName("filename.txt").next().getBlob().getDataAsString().split("\r\n").filter(String);
  var sourceFolderName = "music";
  var destinationFolderId = "folder id 2";
  var source_folder = DriveApp.getFoldersByName(sourceFolderName).next();
  for (var i = 0; i < filenames.length; i++) {
    var filename = filenames[i];
    var file = source_folder.getFilesByName(filename);
    if (file.hasNext()) {
var dest_folder = DriveApp.getFolderById(destinationFolderId);
      var srcFile = file.next();
      var newName = srcFile.getName();
      srcFile.makeCopy(newName, dest_folder);
    }
  }
}

Upvotes: 0

Views: 80

Answers (1)

Tanaike
Tanaike

Reputation: 201673

Answer for Question 1:

When you want to run myFunction2 after myFunction1 was run, how about the following modification? Please think of this as just one of several answers.

Pattern 1:

In this pattern, by running myFunction1, myFunction1 and myFunction2 are run in order.

function myFunction1 () {
  code

  myFunction2(); // Added
}


function myFunction2 () {
  code
}

Pattern 2:

In this pattern, by running main, myFunction1 and myFunction2 are run in order.

function myFunction1 () {
  code
}


function myFunction2 () {
  code
}

// Added
function main() {
  myFunction1();
  myFunction2();
}

If this was not the direction you want, I apologize.

Answer for Question 2:

  • You want to run the function of copyfile by changing the variables of filename, sourceFolderName and destinationFolderId.

I could understand like this. If my understanding is correct, how about the following modification? In this modification, the variables are put in an array which has the object as each element. Please think of this as just one of several answers.

Sample script:

function copyfile(obj) {
  var source_folder = DriveApp.getFoldersByName(obj.sourceFolderName).next();
  var file = source_folder.getFilesByName(obj.filename);
  if (file.hasNext()) {
    var dest_folder = DriveApp.getFolderById(obj.destinationFolderId);
    var srcFile = file.next();
    var newName = srcFile.getName();
    srcFile.makeCopy(newName, dest_folder);
  }
}

// Please run this function.
function main() {
  // Please set this object.
  var object = [
    {filename: "filenames_value1", sourceFolderName: "sourceFolderName_value1", destinationFolderId: "destinationFolderId _value1"},
    {filename: "filenames_value2", sourceFolderName: "sourceFolderName_value2", destinationFolderId: "destinationFolderId _value2"}
  ];
  for (var i = 0; i < object.length; i++) {
    copyfile(object[i]);
  }
}
  • Before you run the script, please set the variables of filename, sourceFolderName and destinationFolderId of the object of object.
  • When you run the script, please run main.

Upvotes: 2

Related Questions