0

I've been trying to find a script I can copy over for my purposes (with some modifications for my specific sheet), but nothing is quite doing what I need it to and I don't have the coding skills to write it from scratch.

I have a Google spreadsheet with multiple sheets where we will constantly be adding new sheets, one for each new product we launch. Each of the sheets (including the new ones, created from a template) have an "At a Glance" summary of that product's launch readiness status. This is always located at M3:M10 on each sheet.

I have a Status Summary sheet where I'd like to paste the information found at M3:M10 from each sheet. This will allow us to quickly see on the Status Summary how each product is doing ahead of launch, without requiring us to check each specific sheet's tab.

This is what I'd ideally like to happen:

  • The script will loop through each sheet contained within the workbook, but skip the sheet called "Status Summary."
  • Script will pull data from M3:M10 from each sheet
  • Script will deposit M3:M10 data on "Status Summary," organizing the information across A2:H2 (my column labels are A1:H1). For example: M3 data on Sheet 1 will be deposited in A2 on Status Summary, M3 data on Sheet 1 will be deposited in B2 on Status Summary, etc.
  • Script will automatically use a new line for each new sheet. For example: M3 data on Sheet 1 will be in A2 on Status Summary, M3 data on Sheet 2 will be in A3 on Status Summary, etc.

I don't need the names of the sheets included.

Is anyone able to write this for me? I'm trying to teach myself how to code these scripts, so a few "//" explanations about what each section does would be a huge help. Thank you for looking!

Adrienne
  • 27
  • 1
  • 6

1 Answers1

0

try this:

function atAGlance() {
  var ss=SpreadsheetApp.getActive();
  var excl=['Status Summary'];
  var sh=ss.getSheetByName('Status Summary');
  var shts=ss.getSheets();
  var n=1;
  for(var i=0;i<shts.length;i++) {
    if(excl.indexOf(shts[i].getName())==-1){
      var v1=shts[i].getRange("M3:M10").getValues();
      var v2=transpose(v1);
      sh.getRange(1 + n++,1,v2.length,v2[0].length).setValues(v2);
    }
  }
}
//https://stackoverflow.com/a/16705104/7215091
function transpose(a)
{
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}

Got the transpose from [https://stackoverflow.com/a/16705104/7215091] (https://stackoverflow.com/a/16705104/7215091)

This version allows you to exclude sheets from the process and you can run it as shown below in the testAtAGlance() function. The example I was using excluded 'Status Summary' and 'Globals'.

function testAtAGlance() {
  atAGlance(['Status Summary','Globals']);
}

function atAGlance(excl) {
  var excl=excl || ['Status Summary'];
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Status Summary');
  var shts=ss.getSheets();
  var n=1;
  for(var i=0;i<shts.length;i++) {
    if(excl.indexOf(shts[i].getName())==-1){
      var v1=shts[i].getRange("M3:M10").getValues();
      var v2=transpose(v1);
      sh.getRange(1 + n++,1,v2.length,v2[0].length).setValues(v2);
    }
  }
}
//https://stackoverflow.com/a/16705104/7215091
function transpose(a)
{
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank you! I'm getting errors, though, and I'm not sure what they mean. sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).clearContent(); //This line has an error of "The coordinates or dimensions of the range are invalid." Can you tell me exactly what this line is doing, so I can try to fix it? var v=sht[i].getRange("M3:M10").getValues(); //This line seems to need the "t" removed from v=sht. I changed it to "v=sh[i]" and that seems to help. Am I correct? Thanks for helping! – Adrienne Mar 25 '19 at 17:02
  • It's functioning, once I changed line 7 "var v=sht[i].getRange" to "var v=shts[i].getRange". But it only pulls information from M3, not M3:M10. I ran it twice and it simply added the information in M3 again. What am I missing? – Adrienne Mar 25 '19 at 18:22
  • Oops sorry for that. – Cooper Mar 25 '19 at 18:23
  • I'm not really sure what to do next with the script, so that it will return the result I'm looking for (the whole range of M3:M10). Am I trying to do something impossible with the script? I've had trouble with ranges on the other scripts I've tried to use for this purpose. It only wants to read data from a single cell, not multiples. – Adrienne Mar 25 '19 at 18:49
  • Okay I think that last change should do it. I finally tested it and figured out what was wrong with it. – Cooper Mar 25 '19 at 19:00
  • This is working! Thank you so much! Is there a way to tell it to overwrite the data it pulled the last time it ran? (So I don't have it stacking information from the same sheets on new lines.) If not, no worries. I can work with it as is. I really appreciate all the help. – Adrienne Mar 25 '19 at 19:12
  • Are saying that you always want it to write to the first row in Summary Sheet? – Cooper Mar 25 '19 at 19:41
  • The second row, but yes, I think so. Always depositing data starting on the second row in column A (A2:H2). It should loop through all the sheets in the worksheet, giving each sheet its own line on Status Summary (which is what it does beautifully!). But if I run it again, instead of updating the same lines with the updated information, it appends new lines for all the sheets a second time, so then I have duplicates. I can work around this by deleting the status lines after each time I've run the script, but I'm trying to make this as user-friendly as possible for non-Excel people. – Adrienne Mar 25 '19 at 19:45
  • Okay that should do it. – Cooper Mar 25 '19 at 19:55
  • Interesting! It does seem to work, but then it overwrites itself while it's running. So the data shows up, then immediately disappears again. – Adrienne Mar 25 '19 at 20:04
  • I didn't think it made much sense because your getting information from every sheets and yes they will overwrite each other. – Cooper Mar 25 '19 at 20:13
  • Hrm. Okay. I think I'll just stick to deleting the data after each Update (script run). Thanks again for all your help! I really appreciate it. – Adrienne Mar 25 '19 at 20:15
  • So I'm getting the feeling that this isn't really what you want. If you can tell me exactly what you want, then I can do it. – Cooper Mar 25 '19 at 20:19
  • This script does 99% of what I was hoping for. It's great! I'm probably not explaining the last bit very well. Every time I run the script, it appends new lines for the same sheets it's taken data from originally. So if I update one of the sheets and run the script a second time, I'll now have 2 lines with different information for the same sheet. I'm trying to find a way for the script to run once, then the second time I run it, it will overwrite the data gleaned from run1 with the data from run2. Maybe that's clearer? – Adrienne Mar 25 '19 at 20:22
  • Okay. What this does is that it goes through all of the sheets that are no in the `excl` array (i.e. the excluded sheets array) and it take the data in each sheet from the range M3:M10 and turns the column into a row and appends below your header line. It starts at row 2 and continues appending row for all of the sheets not in the `excl` (excluded sheets array). So if you have sheets that you don't want to be involved in the process then put then into the `excl` (excluded sheets array) – Cooper Mar 25 '19 at 20:52
  • That makes sense, thanks! What I'm trying to explain is that this script will run triggered by an "Update" button that I've linked it to. When someone goes to the Status Summary tab, they'll click "Update," run the script, and see information on each project. If they change something on one sheet, then click "Update" a second time, they'll receive the new information, but it will be added onto the original information. So the original information isn't updated(replaced), but just added. This script works just fine, if I only make certain we delete the data each time we click "Update." Thanks! – Adrienne Mar 25 '19 at 21:19
  • You should try running the last modification. On the bottom of my answer. That version will replace the data in the same place it was before unless you change the position of the sheets. But in any case each time you run the script it overwrites the old data now. – Cooper Mar 25 '19 at 21:38
  • Oh! That's it exactly. Perfection all around. Thanks again for all your help. – Adrienne Mar 25 '19 at 21:46