0

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?

2
  • 1
    Short answer: Maybe. If you need further help please add more details.
    – Rubén
    Oct 5 at 2:06
  • Hi @Rubén As soon as possible I'll add the scripts to the question for easy viewing.
    – Brondby IF
    Oct 5 at 2:28
1

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

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!
    – Brondby IF
    Oct 5 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
    – Rubén
    Oct 5 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.
    – Brondby IF
    Oct 5 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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