1

Script 1:

Scraping data from a website and puts the new collected data into Column A and Column B.

function scraping() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('PageA');
  var url = 'www.linktoscraping.com';

  const contentText = UrlFetchApp.fetch(url).getContentText();
  const $ = Cheerio.load(contentText);
  
    $('path1')
        .each((index, element) => {sheet.getRange(index + 1, 1).setValue($(element).text().trim());});

    $('path2')
        .each((index, element) => {sheet.getRange(index + 1, 2).setValue($(element).text().trim());});
}

Script 2:

Takes the data from Column A and Column B and puts it in another spreadsheet:

function register() {
  var SheetA = SpreadsheetApp.openById('AAAAAAA').getSheetByName('PageA');
  var data = SheetA.getRange('A1:B').getValues();
  var SheetB = SpreadsheetApp.openById('BBBBBBB').getSheetByName('History');
  SheetB.getRange(1,1, data.length, data[0].length).setValues(data);
}



Both scripts with a trigger activated so that they run every 1 minute.

If both scripts happen to run at the same time, can one of them cause an error in the other's running or can they work simultaneously without problems?

3
  • 1
    Short answer: Maybe. If you need further help please add more details. Commented Oct 5, 2021 at 2:06
  • Hi @Rubén As soon as possible I'll add the scripts to the question for easy viewing. Commented Oct 5, 2021 at 2:28
  • I've upvoted this question, as I think it's important information especially for new users.
    – Robert M.
    Commented Aug 6, 2023 at 17:45

2 Answers 2

2

The scripts shown are very prone to have problems.

The first one has the problem of writing to one cell at a time which is discouraged in https://developers.google.com/apps-script/guides/support/best-practices and since this is set to run every minute, and assuming that you are using a free account, it's very likely that this script will consume all triggers total runtime quota.

Regarding the second script, there is no way to be sure what version of the source spreadsheet will be read, it's prone to throw errors and will cause the all triggers total runtime quota be consumed faster.

I think that the best bet is to use a single trigger that uses batchUpdate (requires the use Advanced Sheets Service) to write the scraped data with a single call to both spreadsheets.

Resources

1
  • This is like what's called atomicity in database systems. You don't want two processes working on the same data at the same time - thus those processes are said to be "atomic transactions" in the sense that they're indivisible.
    – Robert M.
    Commented Aug 6, 2023 at 17:44
2

To make sure that Script1() and Script2() run in the correct order, remove your current triggers, and add this function:

function scriptRunner() {
  Script1();
  Script2();
}

Then install a time-driven trigger that runs scriptRunner().

A one-minute timer may run too frequently. You may want to set the trigger to run once every 15 minutes or so.

3
  • Good morning mate. Exactly because it needs agility that both scripts are activated separately and not together, because together it would take more time to run. That's why my doubt is there is a risk of a script causing slowness, breaks, failures in the execution of the other or something like that, because I honestly don't know if two scripts together can occupy the same space at the same time. I've tokenized your answer with an upvote because it will be very useful to other people, even though it's not specifically the solution to my question. Thank you for your time, thank you so much! Commented Oct 5, 2021 at 12:54
  • 1
    @BrondbyIF If you are looking for agility, make a single script that do a batchUpdate (requires the Sheets advanced service) to update both spreadsheets Commented Oct 5, 2021 at 18:52
  • Hello friends doublenary and @Rubén , I just updated the question, putting the scripts that I use in each of the functions activated by trigger. Commented Oct 5, 2021 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.