将Google管理员用户报告导入Google表格

问题描述

我正在尝试将Google管理员帐户中的用户报告导入Google表格。导出到Google表格很简单,但是我想运行一个Google脚本,将用户报告插入到现有的Google表格中。

解决方法

尝试一下。

function cloneGoogleSheet(existingSheet,userReportSheet){
   //get source google sheet document
   var source = SpreadsheetApp.openById(userReportSheet);
   //get the source sheet on document
   var sourceSheet = source.getSheetByName('NameOfSheet');

   //get data range 
   var dataRange = sourceSheet.getDataRange();

   // get A1 notation for the range identification
   var A1Range = dataRange.getA1Notation();

   //get data within the specified range
   var data = dataRange.getValues();

   //specify the existing spreadsheet you want to pull data to
   var target = SpreadsheetApp.openById(existingSheet);

   //target sheet on your existing document 
   var targetSheet = target.getSheetByName('NameOfTargetSheet');

   //if there is already data on the target sheet,you may want to remove it 
   targetSheet.clear({contentsOnly: true});

   //now you set the target range of the values you get from your user report
   targetSheet.getRange(A1Range).setValues(data);
};