简体   繁体   中英

How do I perform an operation to multiple spreadsheets using “OpenbyID” in google scripts

Please bear with me as I'm a newbie, but I have formed a list of the ID's of the spreadsheets that I have in a folder which I have stored on a sheet in 1 column. I am trying to execute a script that will go into each of those spreadsheets and delete sheets of a given name if they are in there. So far this is what I have but I'm not sure where I screwed up:

function cleanAllOld () {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('fileList');
  var numberFiles = sheet.getLastRow();

  for (var i=0; i<numberFiles; i++) {

    try{

// File to be cleaned
  var fileID = sheet.getRange(2+i, 1, numberFiles-1).getValues();


  var destination = SpreadsheetApp.openById(fileID[i]);
  var sheet1 = destination.getSheetByName('Copy of CALC');
  var sheet2 = destination.getSheetByName('Copy of Print');
  var sheet3 = destination.getSheetByName('Copy of Markbook');

 if (destination.sheet1 == null) {
  } else {
    destination.deleteSheet(sheet1);
  }

  if (destination.sheet2 == null) {
  } else {
    destination.deleteSheet(sheet2);
  }

  if (destination.sheet3 == null) {
  } else {
    destination.deleteSheet(sheet3);
  }

}
    catch (e) {
      Logger.log(e);
    }
}
}

FIXED SCRIPT

     function cleanAllOld () {

  var ss = SpreadsheetApp.openById('...');
  var sheet = ss.getSheetByName('fileList');
  var numberFiles = sheet.getLastRow();

  for (var i=0; i<numberFiles; i++) {

// File to be cleaned
  var fileID = sheet.getRange(1, 1, numberFiles).getValues();
 Logger.log('fileID[i][0]: ' + fileID[i][0]);       
  var destination = SpreadsheetApp.openById(fileID[i][0]);
  var sheet1 = destination.getSheetByName('Copy of CALC');
  var sheet2 = destination.getSheetByName('Copy of Print');
  var sheet3 = destination.getSheetByName('Copy of Markbook');

 if (sheet1 === null) {
  } else {
    destination.deleteSheet(sheet1);
  }

  if (sheet2 === null) {
  } else {
    destination.deleteSheet(sheet2);
  }

  if (sheet3 === null) {
  } else {
    destination.deleteSheet(sheet3);
  } 

}
}

The getValues() method returns a two dimensional array. You are getting an error msg from:

var destination = SpreadsheetApp.openById(fileID[i])

Try using:

var destination = SpreadsheetApp.openById(fileID[i][0])

Add a second index with a zero.

Resolved the issue, I have bolded the corrected line below

  function cleanAllOld () {

  var ss = SpreadsheetApp.openById('...');
  var sheet = ss.getSheetByName('fileList');
  var numberFiles = sheet.getLastRow();

  for (var i=0; i<numberFiles; i++) {

// File to be cleaned
  **var fileID = sheet.getRange(1, 1, numberFiles).getValues();**
 Logger.log('fileID[i][0]: ' + fileID[i][0]);       
  var destination = SpreadsheetApp.openById(fileID[i][0]);
  var sheet1 = destination.getSheetByName('Copy of CALC');
  var sheet2 = destination.getSheetByName('Copy of Print');
  var sheet3 = destination.getSheetByName('Copy of Markbook');

 if (sheet1 === null) {
  } else {
    destination.deleteSheet(sheet1);
  }

  if (sheet2 === null) {
  } else {
    destination.deleteSheet(sheet2);
  }

  if (sheet3 === null) {
  } else {
    destination.deleteSheet(sheet3);
  } 

}
}

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