title: Only run Appscript function if certain cell has specific value in Google Sheets
body: <p>I have a function in Google AppsScript that copies the current sheet and paste in another file:</p>
  <pre><code>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 =&gt; {
      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}&amp;exportFormat=xlsx`, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();
  
    // 5. Crete the blob as a file.
  
    var folder = DriveApp.getFolderById(&quot;XXXXXXX&quot;);
    var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(&quot;N1&quot;).getValue()+&quot;.xlsx&quot;;
    folder.createFile(xlsxBlob.setName(filename));
    // 6. Delete the temporate Spreadsheet.
    DriveApp.getFileById(id).setTrashed(true);
  
    var folder = DriveApp.getFolderById(&quot;YYYYYYYYY&quot;);
  
    var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(&quot;N1&quot;).getValue()+&quot;.xlsx&quot;;
    folder.createFile(xlsxBlob.setName(filename));
  
    // 6. Delete the temporate Spreadsheet.
    DriveApp.getFileById(id).setTrashed(true);
  
    // 7. Save the whole Spreadsheet in repository
    var folderId = &quot;ZZZZZZZZZZ&quot;; 
  
    const spreadsheet2 = SpreadsheetApp.getActiveSpreadsheet();
    var filename2 = spreadsheet2.getActiveSheet().getRange(&quot;N1&quot;).getValue();
  
    var folder = DriveApp.getFolderById(folderId);
    DriveApp.getFileById(spreadsheet2.getId()).makeCopy(filename2, folder);  
  
  }
  </code></pre>
  <p>However I want to include an if statement in the start like &quot;IF B2 != 'Ok' don't run the script and print some error message&quot; (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?</p>