0

after previous post Script setValue based on the values of column based on value of other column matching reference

where i collected sheet IDs, currently almost 300

im trying to edit ALL those spreadsheets from theirs IDs

function update()
{
  const ss = SpreadsheetApp.getActiveSpreadsheet();           // Source spreadsheet
  const src = ss.getSheetByName('Relação');                   // Source sheet

  const LR = src.getLastRow();

  const Target_Id = src.getRange("B2:B" + LR).getValues();    // Target IDs

  for (var i = 0; i < LR; i++)
  {
    if ( Target_Id[i] !== "" )                                // check to skip "" rows
    {
      var ss_t = SpreadsheetApp.openById(Target_Id[i]);       // Target spreadsheet
      var trg = ss_t.getSheetByName('Config');                // Target sheet

      trg.getRange("B24").setValue("TEST");
    }
  }
}

and it works untill one row in column B is "" blank

Exception: Invalid argument: id
update  @ script.gs:14

aparently my check

if ( Target_Id[i] !== "" )

isnt working the way i tried, dont really understand why, thats my main question

question 2: as i tested, i know since im trying to update almost 300 sheets, im gonna run into another problem

Exceeded maximum execution time

to update everyting is going to be slow, hit that error around ID 40, so is there a way to optimize the process so i can be able to update all spreadsheets? for now im tying to just update 1 cell, as proof of concept, end goal is entire sheets with formulas, formating, etc but thats a topic for future post if needed

  • are you sure blank cells are passed as empty strings `""`? Try debugging or logging the `Target_Id[i]` and see if it is instead `undefined` or `null`. – pilchard Mar 13 '22 at 00:49
  • @pilchard i made it so the 2nd row (in this case B3) be "", debuggind array 0 shows correct id, array 1 shows "", array 2 shows correct id, tried to change line 12 to if ( Target_Id[i] !== "" || Target_Id[i] !== null ) and got the same result, have a check using IMPORTRANGE and only the fist line B2 changes, ive dont this and it always stops at the first row "" – Bruno Carvalho Mar 13 '22 at 01:59
  • not sure if its relevant to that case but its a test spreadsheet, where i get all IDs with IMPORTRANGE, the correct IDs and blank ones are exatly as on the original spreadsheet, same rows – Bruno Carvalho Mar 13 '22 at 02:02
  • 2
    In your script, I thought that when `Target_Id[i] !== ""` is modified to `Target_Id[i][0] != ""`, the script works. But about your `question 2`, I think that this is a difficult question. in this case, it is required to know your detailed situation. Because I'm worried that in the current stage, there are no methods for efficiency accessing 300 Spreadsheets in one run. In your situation, how do you want to run your script? For example, if you directly run the script from the script editor, I thought that a workaround might be able to be used. – Tanaike Mar 13 '22 at 02:38
  • thanks @Tanaike it worked, but it just edited 30 sheets as expected, those individual spreadsheets are completely autonomous with their own scripts and triggers, to track brazilian stocks, but its a WIP, where i continuously work on a template, when a milestone is reached i recreate all sheets, thats what im trying to do, edit instead of recreating everyting, but its not something that is going to be used on daily basis, far from it, at most a few times per year, manually from script editor – Bruno Carvalho Mar 14 '22 at 01:39
  • Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, I cannot understand your reply. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I would be grateful if you can forgive my poor English skill. – Tanaike Mar 14 '22 at 03:33
  • 1
    Hi, from `thanks @Tanaike it worked, but it just edited 30 sheets as expected` your first problem was solved, and now you just want to avoid `Exceeded maximum execution time`? For that problem, see for example this answer: https://stackoverflow.com/a/61057513. If that is not useful, please consider providing more information about your current situation. – Iamblichus Mar 14 '22 at 10:41
  • thanks @Iamblichus will check it and if needed will make another post with detailed information – Bruno Carvalho Mar 14 '22 at 21:38
  • dont wotty @Tanaike i just ried to explain in comments, and it never goes well, i will try to investigate a bit further and will make another post with detailed info in future. ALSO, as you have fixed my first problem, if you want, make a reply and i will flag it as answered, thanks again buddy – Bruno Carvalho Mar 14 '22 at 21:41
  • Thank you for replying. I understood it. So, I post it as an answer. Could you please confirm it? And, when I saw your new question, I would like to check it. – Tanaike Mar 14 '22 at 23:52

1 Answers1

2

In your script, please modify your script as follows.

From:

Target_Id[i] !== ""

To:

Target_Id[i][0] != ""
  • The value retrieved by getValues() is 2 dimensional array. By this, I proposed the above modification.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165