Home > Software engineering >  Only run Appscript function if certain cell has specific value in Google Sheets
Only run Appscript function if certain cell has specific value in Google Sheets

Time:03-10

I have a function in Google AppsScript that copies the current sheet and paste in another file:

function exportSheet() {
  const exportSheetName = 'DF';  // Please set the target sheet name.

  // 1. Copy the active Spreadsheet as a tempora Spreadsheet.
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet().copy('tmp');

  // 2. Convert the formulas to the texts.
  const targetRange = spreadsheet.getSheetByName(exportSheetName).getDataRange();
  targetRange.copyTo(targetRange, {contentsOnly:true});

  // 3. Delete the sheets except for a sheet you want to export.
  spreadsheet.getSheets().forEach(sheet => {
    if (exportSheetName != sheet.getName()) spreadsheet.deleteSheet(sheet)
  });

  // 4. Retrieve the blob from the export URL.
  const id = spreadsheet.getId();
  const xlsxBlob = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/export?id=${id}&exportFormat=xlsx`, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();

  // 5. Crete the blob as a file.

  var folder = DriveApp.getFolderById("XXXXXXX");
  var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("N1").getValue() ".xlsx";
  folder.createFile(xlsxBlob.setName(filename));
  // 6. Delete the temporate Spreadsheet.
  DriveApp.getFileById(id).setTrashed(true);

  var folder = DriveApp.getFolderById("YYYYYYYYY");

  var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("N1").getValue() ".xlsx";
  folder.createFile(xlsxBlob.setName(filename));

  // 6. Delete the temporate Spreadsheet.
  DriveApp.getFileById(id).setTrashed(true);

  // 7. Save the whole Spreadsheet in repository
  var folderId = "ZZZZZZZZZZ"; 

  const spreadsheet2 = SpreadsheetApp.getActiveSpreadsheet();
  var filename2 = spreadsheet2.getActiveSheet().getRange("N1").getValue();

  var folder = DriveApp.getFolderById(folderId);
  DriveApp.getFileById(spreadsheet2.getId()).makeCopy(filename2, folder);  

}

However I want to include an if statement in the start like "IF B2 != 'Ok' don't run the script and print some error message" (I know it sounds beginner, that's because I am a beginner indeed). What is the right synthax for that? How could I put my previous function inside the if sentence and also print an error message?

CodePudding user response:

Description

Simply put a test of cell B2 at the beginning of your script. And display an error message.

Script

function exportSheet() {
  const exportSheetName = 'DF';  // Please set the target sheet name.

  // 1. Copy the active Spreadsheet as a tempora Spreadsheet.
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  if( spreadsheet.getActiveSheet().getRange("B1").getValue() !== "Ok" ) {
    SpreadsheetApp.getUi().alert("oops");
    return;
  }
  else {
    spreadsheet.copy("tmp");
  }
  //  the rest of your code
}

Reference

  • Related