简体   繁体   English

为 4 个工作表选项卡寻找一个 Google Script 应用程序以生成一个 json

[英]Looking for one Google Script App for 4 sheet tabs to produce one json

Ok...not sure how to do this.好吧...不知道如何做到这一点。 Right now I 4 sheets and 4 scripts for each sheet producing 4 json feeds.现在我为每张纸制作 4 张纸和 4 个脚本,生成 4 个 json 提要。 What I am trying to experiment with is having one script that will produce 1 json that I can use in a web page and just call the type of class.我正在尝试使用一个脚本来生成 1 个 json,我可以在网页中使用它并调用类的类型。 They are all formatted the same with columns etc.它们的格式都与列等相同。

Here is the Google Script App code I have.这是我拥有的 Google Script App 代码。

    function doGet(e){

 // Change Spread Sheet url
 var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/SpreadsheetID/edit#gid=0");

// Sheet Name, Change Sheet to whatever name you used to name your sheet
 var sheet = ss.getSheetByName("Class Sheet 1");

 return getClasses(sheet); 

}

function getClasses(sheet){
  var dataObj = {};
  var dataArray = [];

// collecting data starting from 2nd Row , 1st column to last row and last column
  var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).sort([{column: 1, ascending: true}, 1]).getValues();

  for(var i = 0, l= rows.length; i<l ; i++){
    var dataRow = rows[i];
    var record = {};

    record['Name'] = dataRow[0];
    record['Note'] = dataRow[1];
    record['Address'] = dataRow[2];
    record['StreetAddress'] = dataRow[3];
    record['City'] = dataRow[4];
    record['State'] = dataRow[5];
    record['ZipCode'] = dataRow[6];
    record['ContactName'] = dataRow[7];
    record['EMailAddress'] = dataRow[8];
    record['CustomerServicePhone'] = dataRow[9];

    dataArray.push(record);

  }

  dataObj = dataArray;

  var result = JSON.stringify(dataObj);

  return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);

 } 

Scratching my head on this a little bit....I'm sure its something simple and I am probably over thinking things, but any help would be appreciated.对此有点挠头……我确定它很简单,我可能想多了,但任何帮助将不胜感激。

Possible Solution:可能的解决方案:

The e object in your doGet(e) provides a way to send parameters to your script. doGet(e)e对象提供了一种向脚本发送参数的方法。 You can access different sheets with different url parameters.您可以使用不同的 url 参数访问不同的工作表。 You can then easily get the requested SheetName through e.parameter .然后您可以通过e.parameter轻松获取请求的e.parameter Use

https://script.google.com/.../exec?sheet=ClassSheet1 //for ClassSheet1
https://script.google.com/.../exec?sheet=ClassSheet2 //for ClassSheet2

Code.gs:代码.gs:

function doGet(e){
 var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/SpreadsheetID/edit#gid=0");
 var sheetName = e.parameter.sheet;
 var sheet = ss.getSheetByName(sheetName);
 return getClasses(sheet); 
}

You can also provide UI in your web-app to select a sheet.您还可以在 Web 应用程序中提供 UI 以选择工作表。

暂无
暂无

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

相关问题 将一张工作表中的原始数据排序到Google App脚本中新工作表中的单独标签中 - Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? 使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢 - Slow Execution for Copying Data from One Sheet to Another Using Google App Script 在我创建新工作表之前,Google Apps 脚本会复制工作表 - Google Apps Script copies a sheet before I create a new one Google电子表格脚本。 仅适用于一张纸 - Google Spreadsheet Script. Only work for one sheet 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script Google电子表格脚本-仅授予对一张工作表的访问权限 - Google spreadsheets script - grant access to only one sheet Google表格脚本编辑器-一种如何正确格式化其数字? - Google Sheet Script Editor - how would one format their numbers properly? Google Apps脚本在一个工作表中复制一列,将其转置,然后粘贴到另一个 - Google Apps Script Copying a column in one sheet, transposing it, then pasting to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM