简体   繁体   中英

Run script in specified sheet in Google Sheets

I have made a speadsheet with Google Sheet which contains two sheets:

Name of sheet 1: Start
Name of sheet 2: Playlist

I have made the following script:

function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A4:C15");

// Randomizes the range
range.randomize();
}    

The script worked fine when I only had 1 sheet. Now I have two and I want the script to run on the sheet named Playlist.

I can't figure out how to do this. Please help.

Thanks.

If you only want to run the code on the "Playlist" sheet this should work for you.

function shuffleSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Playlist");
  var range = sheet.getRange("A4:C15");

  // Randomizes the range
  range.randomize(); 
}

I you want to run it on all sheets then you need to use a for loop.

function shuffleSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

  for (var i = 0; i < sheets.length; i++) {
    var sheet = sheets[i];
    var range = sheet.getRange("A4:C15");

    // Randomizes the range
    range.randomize(); 
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM