简体   繁体   English

Google Apps 脚本如何将特定工作表导出为 csv 格式

[英]Google Apps Script how to export specific sheet as csv format

In MIT Inventor II, I use web component to get SpreadsheetID and SheetID through doGet() of google apps script.在 MIT Inventor II 中,我使用 web 组件通过 google apps 脚本的 doGet() 获取 SpreadsheetID 和 SheetID。 After I get the information I use another web component to set url as below to get csv-formatted file from specific sheet.获得信息后,我使用另一个 web 组件设置 url,如下所示,以从特定工作表中获取 csv 格式的文件。 My question is how to make GAS to get SpreadsheetID & SheetID and then export csv file at one time, so that I don't have to use 2 web components in Inventor side?我的问题是如何让 GAS 获取 SpreadsheetID 和 SheetID,然后一次导出 csv 文件,这样我就不必在 Inventor 端使用 2 web 组件?

GAS codes is as below. GAS代码如下。 This is to "return" spreadsheetID and sheetID.这是为了“返回”电子表格 ID 和工作表 ID。

function doGet(e) {

filename = e.parameter.account;

fileList = DriveApp.getFilesByName(filename);

while (fileList.hasNext()) {
var fileID = fileList.next().getId()
    }

var file = SpreadsheetApp.openById(fileID) ;

   sheet = file.getSheetByName("Message").activate()

  var messageID = sheet.getSheetId();  

return ContentService.createTextOutput([fileID,messageID]);

After I got SpreadsheetID & SheetID, I have to set 2nd web component from Inventor side to get csv file, see below.在获得 SpreadsheetID 和 SheetID 后,我必须从 Inventor 端设置第二个 web 组件以获取 csv 文件,见下文。

https://docs.google.com/spreadsheets/d/xxxxSpreadsheetIDxxxx/edit#gid=sheetID

Here is how you can create a csv file of a selected sheet in google drive:以下是如何在谷歌驱动器中创建所选工作表的 csv 文件:

function sheetToCsv()
{
    var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
    var sheet_Name = "Sheet1"
  
  
    var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
  
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_Name)
      var sheetNameId = sheet.getSheetId().toString();
  
      params= ssID+"/export?gid="+sheetNameId +"&format=csv"
      var url = "https://docs.google.com/spreadsheets/d/"+ params
      var result = UrlFetchApp.fetch(url, requestData);  
  
   var resource = {
  title: sheet_Name+".csv",
  mimeType: "application/vnd.csv"
     }
   var fileJson = Drive.Files.insert(resource,result)
  
} 

The code creates a csv file that has the content of Sheet1 .该代码创建了一个 csv 文件,其中包含Sheet1的内容。

In order to run the aforementioned function you need to activate the Advanced Drive Service .为了运行上述 function 您需要激活Advanced Drive Service

Explanation:解释:

Go to Resources => Advanced Google Services => activate Drive API Go 到资源=>高级 Google 服务=>激活驱动器 API

Another option is to create the csv file to a particular folder , then you need to replace the resource part of the code with this:另一种选择是将 csv 文件创建到特定文件夹,然后您需要将代码的资源部分替换为:

var folder_id ='id';
       
   var resource = {
  title: sheet_Name+".csv",
  mimeType: "application/vnd.csv",
  parents: [{ id: folder_id }]
     }

My application was how to save a tab of a Google sheets spreadsheet to a CSV file in a shared drive.我的应用程序是如何将 Google 表格电子表格的选项卡保存到共享驱动器中的 CSV 文件。 Doing it to the default "My Drive" was relatively easy based on Marios' answer in this post , but I struggled with this for a shared drive while until I came across ziganotschka's example which solved my problem.根据Marios 在这篇文章中的回答,对默认的“我的驱动器”进行操作相对容易,但我一直在努力解决共享驱动器的问题,直到我遇到ziganotschka 的示例解决了我的问题。

Code for my simple App Script is:我的简单应用程序脚本的代码是:

function sheetToCsv()
{
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheet_Name = "[Your sheet name goes here]";

var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_Name);
var sheetNameId = sheet.getSheetId().toString();

params= ssID+"/export?gid="+sheetNameId +"&format=csv";
var url = "https://docs.google.com/spreadsheets/d/"+ params;
var result = UrlFetchApp.fetch(url, requestData);  

var resource = {
  title: sheet_Name+".csv",
  mimeType: "MimeType.CSV",
  parents:[{
  "id": "[Your Shared Drive Folder ID goes here]"
      }]
    }

var optionalArgs={supportsAllDrives: true};

var fileJson = Drive.Files.insert(resource, result, optionalArgs);

} 

Next step is to add timestamp to the file name and cause the script to execute periodically using a timer.下一步是将时间戳添加到文件名,并使用计时器使脚本定期执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Google Apps脚本中导出到CSV时保持日期格式 - How to maintain date format in export to CSV in Google Apps Script Google Apps 脚本 - 格式化特定工作表中的列 - Google Apps Script - Format Column in Specific Sheet 使用 Apps 脚本以 Excel 格式将 Google Sheet 导出到 Google Drive - Export a Google Sheet to Google Drive in Excel format with Apps Script 将Google Apps脚本结果导出到Google表格 - Export Google Apps Script result to Google Sheet Apps 脚本 - 将 JSON 正确导出到 Google 表格中 - Apps Script - Export JSON properly into Google Sheet Google Apps 脚本如何按名称删除特定工作表 - How can Google Apps Script to delete a specific sheet by name 如何在 Google Apps 脚本中通过 id 获取特定工作表? - How to get specific sheet by id in Google Apps script? Google Apps脚本:将仪表板导出到CSV - Google Apps Script: Export Dashboard to CSV 如何使用谷歌应用脚本在特定范围内从工作表中获取特定图表并将其粘贴到另一张工作表上? - How to get a specific chart from a sheet and paste it on another sheet, in a specific range using google apps script? 如何使用 Google Apps 脚本将表格从 Google 表格上传到 AWS S3 作为 CSV? - How to upload a sheet from Google Sheets to AWS S3 as a CSV using google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM