Cannot run two functions in one script

40 views
Skip to first unread message

Franz

unread,
Dec 22, 2019, 10:19:21 AM12/22/19
to Google Apps Script Community
Hi everyone,

I would need some help with a GAS as it's not working as expected. I'm not a developer, so I tried my best and I recorded a macro in Google Sheet that I then modified, but basically if I run the functions in one script it deletes everything, instead if I split the script and I run the functions one by one, it works perfectly.

Here you can find a Sheet with the script and an example.

I have a list of IDs (first tab) that I need to copy, in groups of 5, into individual cells. I created a formula to do this and a first script (Part 1) that does it automatically when run (tab 2).

As I only need these grouped IDs, a second script (Part 2) copies and pastes the values, removed the duplicates and and extra row and the final result become perfect (tab 3).

I just wanted to combine these two functions into one script, but if i run the two functions together, id doesn't work. I found some posts where they shared some advice, but it still doesn't work.

Can anyone help? Here is the script:


function _5reservationspercell0() {
  _5reservationspercell1();
  _5reservationspercell2();
}

function _5reservationspercell1() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('C1').activate();
  spreadsheet.getCurrentCell().setFormula('=JOIN(CHAR(10),$A1:$A5)');
  spreadsheet.getRange('C1:C5').activate();
  spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('C:C'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getRange('C:C').activate();
}

function _5reservationspercell2() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('C:C').activate();
  spreadsheet.getRange('C:C').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.getRange('A:B').activate();
  spreadsheet.getActiveSheet().deleteColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
  spreadsheet.getActiveRange().removeDuplicates().activate();
  spreadsheet.getRange('2:2').activate();
  spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
};




Thank you so much in advance! Happy holidays,

Franz

Tanaike

unread,
Dec 22, 2019, 7:39:56 PM12/22/19
to google-apps-sc...@googlegroups.com
  • You want to summarize the cells of the column "A" every 5 cells.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:
I think that the reason of your issue is that the result is not reflected after `_5reservationspercell1()` was run. By this, when `_5reservationspercell2()` is run, the expected result cannot be retrieved.

Modified script:
When above point is reflected to your script, please modify as follows.

function _5reservationspercell0() {
  _5reservationspercell1
();
 
SpreadsheetApp.flush();  // Added
  _5reservationspercell2
();
}


Reference:

Franz

unread,
Dec 23, 2019, 2:12:47 AM12/23/19
to Google Apps Script Community

Thank you so much, Tanaike! It works perfectly now! :-)

Thanks again and happy holidays!

Franz

Tanaike

unread,
Dec 23, 2019, 3:22:41 AM12/23/19
to Google Apps Script Community
Thank you for replying. I'm glad your issue was resolved. Thank you, too.

Reply all
Reply to author
Forward
0 new messages