0

I have followed the instructions here: how to run google app script function from another project

with the intention of running a function to take place in project 1, from project 2.

What is happening though, is the function is taking place in project 2.

Is this a normal behaviour, or do I need to give a file ID in my function in project 1 or something like that?

Any advice would be gratefully received! Thanks

Example function code:

function loadSgdata() {

  SpreadsheetApp.getActive().toast("Importing Subgrade Data...");
  
  var FldrId = '10flFnQPAygo3PAkja9Gymm_RlZYMQHXh';
  var FileName = 'SGMaster.rpt';
  var sheet_Name = "Import Raw SG" 
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_Name)
  ss.getRange('A1:A').clearContent();
  
  //Reference to the folder    
  var fldr = DriveApp.getFolderById(FldrId);

  //Get files by that name
  var allFilesInFolder = fldr.getFilesByName(FileName);
  
  Logger.log('allFilesInFolder: ' + allFilesInFolder);

  if (allFilesInFolder.hasNext() === false) {
  return false;
  };
  
  var cntFiles = 0;
  //Even if it's only one file, must iterate a while loop in order to access the file (Google drive will allow multiple files of the same name)
  var delimiter = "\t"; // Added
  while (allFilesInFolder.hasNext()) {
  var thisFile = allFilesInFolder.next();
  cntFiles = cntFiles + 1;
  Logger.log('File Count: ' + cntFiles);

  
  var docContent = thisFile.getBlob().getDataAsString(); // Modified
  Logger.log('docContent : ' + docContent );
  var csv = Utilities.parseCsv(docContent, delimiter); // Added
  ss.getRange(ss.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv); // Added
}

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_Name)
  r1=sheet.getRange("A26:A")
  r2=sheet.getRange("A26:A")

  data=r1.getValues();

  for (i=0;i<data.length;i++)
  {
    data[i][0] = data[i][0].toString().substring(0,6) + data[i][0].toString().substring(14);
  }
  r2.setValues(data);

}
Jon Bowles
  • 119
  • 6
  • An example would be greatly appreciated [mcve] – Cooper Jul 16 '21 at 00:16
  • Hi Cooper, I have added an example that has been published to a library and ran from a second project, but when ran it just tries to run the function within the second project... – Jon Bowles Jul 16 '21 at 00:42
  • What is the problem with it? – Cooper Jul 16 '21 at 00:50
  • It runs the function in project 2, but I want it to run in project 1 – Jon Bowles Jul 16 '21 at 00:54
  • It's not clear to me what's in project1 and what's in project2 – Cooper Jul 16 '21 at 01:15
  • The function above is in project 1 shared as a library to project 2. I am then running it from project 2 using this: function runImportEngineers() { Conformance.loadSgdata() } But it runs the function in project 2, not project 1. – Jon Bowles Jul 16 '21 at 01:18

1 Answers1

0

The function above is in project 1 shared as a library to project 2. I am then running it from project 2 using this: function runImportEngineers() { Conformance.loadSgdata() } But it runs the function in project 2, not project 1.

What you described in your comment is exactly what is supposed to happen when using a library.

Check out the resource scoping reference section on libraries here.

Resource Shared* Not-Shared** Notes
Sites, Spreadsheets and other containers A call to getActive() returns the container of the including script.

* This means that the library does not have its own instance of the feature/resource and instead is using the one created by the script that invoked it.

** This means that library has its own instance of the resource/feature and that all scripts that use the library share and have access to that same instance.

You may have a valid need for what you're trying to do, but given the information presented, I don't see a need for a library. You can have the loadSgdata() function living in project 2, but instead of using getActive(), use something like openById() to open the project 1 spreadsheet.

Diego
  • 9,261
  • 2
  • 19
  • 33
  • Thanks Diego, I thought I must be able to get by with something like that, but ended up going down the library rabbit hole! Thanks again – Jon Bowles Jul 16 '21 at 07:05