title: Apps Script output to defined range instead of entire sheet
body: <p>unexperienced coder here with 1st post. Managed to adapt a script to my liking but get stuck with this range vs sheet issue, I hope someone can help.</p>
  
  <p>It's a script that lists all files &amp; folder in Google Drive by FolderID. The script creates a new Tab for the folder name, or updates the sheet if that tab already exist.</p>
  
  <p>I think the script uses</p>
  
  <pre><code>sheet.clearContent();
  sheet.appendRow(data);
  </code></pre>
  
  <p>To write content to the sheet.</p>
  
  <p>The script effectivly only writes to 4 columns, A:D. I've been trying to limit the output of the script to column A:D, so the rest of the sheet remains untouched when rerunning the script. Currently the entire sheet gets <code>clear.Content(ed)</code> when rerunning the script.</p>
  
  <p>I've been trying things like <code>sheet.getRange(A1:D).clearContent()</code> and defining the range in combination with <code>range.setValues(Data)</code> but I'm not able to get desired results.</p>
  
  <p>this is the script I have that works, please help me to only output in columns A:D without altering or noticing whatever is in other columns</p>
  
  <pre><code>// Create menu button with function items
  function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Fetch Files')
        .addItem('Files &amp; Folders', 'ListAll')
        .addItem('Folders Only', 'ListFolders')
        .addToUi();
  }
  
  var IDCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Setup"); //Set Sheet name which contains Folder ID
  var folderId = IDCell.getRange("C2").getValue();  //Set Cell that contains Folder ID
  
  // Main function 1: List all folders, &amp; write into the current sheet.
  function ListFolders(){
    getFolderTree(folderId, false);
  };
  
  // Main function 2: List all files &amp; folders, &amp; write into the current sheet.
  function ListAll(){
    getFolderTree(folderId, true); 
  };
  
  // Get Folder Tree
  function getFolderTree(folderId, listAll) {
    try {
      // Get folder by id
      var parentFolder = DriveApp.getFolderById(folderId);
  
      // Initialise the sheet - Create sheet with folder name if there is none
      var ss = SpreadsheetApp.getActive();
      var file, data, sheet = ss.getSheetByName(parentFolder.getName());
        if (sheet) {
      Logger.log("found");
    }
    else {
      sheet = ss.insertSheet(parentFolder.getName());
    }
      // Clear range &amp; set headers
      sheet.clearContent();
      sheet.appendRow(["Full Path", "Name", "URL", "Type"]);
  
      // Get files and folders
      getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
  
    } catch (e) {
      Logger.log(e.toString());
    }
  };
  
  // Get the list of files and folders and their metadata in recursive mode
  function getChildFolders(parentName, parent, data, sheet, listAll) {
    var childFolders = parent.getFolders();
  
    // List folders inside the folder
    while (childFolders.hasNext()) {
      var childFolder = childFolders.next();
      // Logger.log("Folder Name: " + childFolder.getName());
      data = [ 
        parentName + "/" + childFolder.getName(),
        childFolder.getName(),
        childFolder.getUrl(),
        ];
  
      // Write
      sheet.appendRow(data);
  
      // List files inside the folder
      var files = childFolder.getFiles();
      while (listAll &amp; files.hasNext()) {
        var childFile = files.next();
        // Logger.log("File Name: " + childFile.getName());
        data = [ 
          parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
          childFile.getName(),
          childFile.getUrl(),
          childFile.getMimeType(),
          ];
  
        // Write
        sheet.appendRow(data);
      }
  
      // Recursive call of the subfolder
      getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
    }
  };
  </code></pre>