创建新文件夹并将 SHEET1 保存到新文件夹

Create New Folder and Save SHEET1 to the New Folder

我找到了创建新文件夹并将跨页副本 sheet 保存到新文件夹的脚本。

  1. 我想只保存 SHEET1 但我想不通
  2. 脚本在我的云端硬盘中创建新文件夹 => 但我想在 SHARE DRIVE / NEW CLIENT 中创建新文件夹
var ss               = SpreadsheetApp.getActiveSheet();
var dest_folder_name = ss.getRange('A1').getValue();
var copy_name        = ss.getRange('A2').getValue();

var dest_folder      = "";
var file_id          = SpreadsheetApp.getActiveSpreadsheet().getId();
var file             = DriveApp.getFileById(file_id);
var parent_folder    = file.getParents().next();
var sub_folders      = parent_folder.getFolders();

// search a destination folder in current folder
while (sub_folders.hasNext()) {
    dest_folder = sub_folders.next();
    if (dest_folder.getName() === dest_folder_name) {
        break;
    }
    dest_folder = "";
}

// create the destination folder if nothing was found
if (dest_folder === "") {
    dest_folder = parent_folder.createFolder(dest_folder_name);
}

// copy the spreadsheet to the destination folder
file.makeCopy(copy_name, dest_folder);

此致, tt3

已编辑

你问题的第一部分:
回答了类似的问题

你问题的第二部分:
据我所知,您有目标传播的文件 ID sheet,并且您知道来源 sheet 的名称。然后试试这个:

function copyToSpreadsheet(){

 var spreadsheet = SpreadsheetApp.openById("Your Spreadsheet ID");
 var sheet = spreadsheet.getSheetByName("SheetX");

 var destination = SpreadsheetApp.openById('Destination ID');
 sheet.copyTo(destination);
}

您可以使用电子表格 URL 将 /edit#gid=somegidhere 替换为 /export?gid=somegidhere 并使用 FetchApp fetch() and use DriveApp createFile() 上传文件进行下载。

试试这个:

function myFunction() {

    
  var dest_folder_name = "66794562"
  var dest_folder      = "";
  var file_id          = SpreadsheetApp.getActiveSpreadsheet().getId();
  var file             = DriveApp.getFileById(file_id);
  var parent_folder    = file.getParents().next();
  var sub_folders      = parent_folder.getFolders();

  // search a destination folder in current folder
  while (sub_folders.hasNext()) {
      dest_folder = sub_folders.next();
      if (dest_folder.getName() === dest_folder_name) {
          break;
      }
      dest_folder = "";
  }

  // create the destination folder if nothing was found
  if (dest_folder === "") {
      dest_folder = parent_folder.createFolder(dest_folder_name);
  }

  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sh.getSheetByName("Sheet1");
  var spreadsheetId = sh.getId();
  var sheetId = sheet.getSheetId();

  var url           = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?gid='+sheetId;
  var token         = ScriptApp.getOAuthToken();
  var response      = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  var fileName = 'copyofSheet1';
  var blob   = response.getBlob().setName(fileName);
  dest_folder.createFile(blob);

}

来源:

目的地:

内容: