将数据从多个Google电子表格路由到一个主表中的最佳方法是什么?

时间:2019-02-06 21:25:23

标签: javascript google-apps-script google-sheets

我有一个初始的Form Response数据表,该数据表根据业务的不同工作流分为单独的电子表格。现在,我需要将所有这些数据从单个Google表格中收集到一个Master表格中。数据将继续添加到各个工作表中,并且在将数据添加到多个文档中时,需要在重组主文件中进行更新。

function importVariations() { // This function knows about the documents and requests that data.
var documents = [
'1f_2KYg8l_Pnrruc6o2OnmSvz6JUml5eMboRTdSO1PZM', 
'1Vbg7iUgtJtO15W0Bc9x22ImCepfs7F3B3GfnQSbZ1EM',
'1eIK2tiiB28b-wP8so_zbcdNKrF-v0OuXwG4BG1hlI98',
'1jJ6sfzfoCtvFo-zHiQR7vvhukBQC8aiO1ZJEHz5sKwI',
'1EiP0wYAAYpG1VBDq2aaeuwifB2__ZkmwOP7cKdkqWBg',
'17kv6rZtLbMo3dS7vuJNONkH0LWBrfwr_qBi1skeo4WY',
'1bQHXHcSTri_AgBLR932KU6Y0NNr_PIAYJY-KFMKRmFk',
'1jGJqmp-4SbgpMokxv6pyDkbkVSJnlhGYiM82PUQB3mo',
'14oAR3GPW8RKGzmXKhuwBW6CrHBq8xYu_KACP1zRFjlU',
  ];

var sheetsToPostTo = ['1dXyxH5XrqRn9mLXEAOglmg-hFSER0LJa2Df6C5YRtJg'];
var allData = getAllData(documents); // get all the data and store in variable

for (var i = 0; i < sheetsToPostTo.length(); i += 1) { // loop through all master sheets and post allData
    postAllData(allData, sheetsToPostTo[i]);
    }

}

function getAllData(documents) { // concatenate all data and return
var allData = [];

for (var i = 0; i < documents.length; i += 1) { // loop through all documents
    allData = allData.concat(getRawData(documents[i])) // get raw data for each document loop through
    }

return allData; // return all data
}

function getRawData(sheetID) { // get raw data for a sheet, remove headers and return just data
var data = SpreadsheetApp.openbyid(sheetID).getsheetbyname('Responses').getDataRange().getValues();

data.shift();
data.shift();

return data;
}

function postAllData(data, sheetId) { // posts data passed to the sheet id passed
SpreadsheetApp
    .openbyid(sheetId)
    .getsheetbyname('1dXyxH5XrqRn9mLXEAOglmg-hFSER0LJa2Df6C5YRtJg')
    .getRange(3, 1, data.length(), data[i].length())
    .setValues(data); // post all of the data to the sheet
}

遇到错误:

  

TypeError:在对象SpreadsheetApp中找不到函数openbyid。 (第34行,文件“ getdata”)

另外,var sheetsToPostTo不能突出显示为变量,这是为什么?

1 个答案:

答案 0 :(得分:0)

案件很重要。 openById()getSheetByName存在相同的问题。通常,如果变量在脚本编辑器中未突出显示为变量,则意味着代码在某处存在错误。