Import Google Admin User report to Google Sheets - google-apps-script

I am trying to import the User Report in a Google Admin account into Google Sheets. It is straightforward to export to a Google Sheet, but I would like to run a Google script that will pull in the User Report into an existing Google Sheet.

Try this.
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);
};

Related

Is there a way to duplicate data from one google sheet doc to another google sheet doc, when data is added

I have a jot form that submits data to one dedicated google sheet (DGS). I work primarily with one google sheet doc that copies cell data from the DGS. I'm trying to implement a code that when new data is entered to duplicate data and send to the google sheet doc I use regularly.
*Note. If I try to alter the DGS, it throws an error and distorts the data sent.
function copydata()
{
var sourceSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var targetSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var rangeToCopyFrom = sourceSheet.getRange(sourceSheet.getLastRow(), 2, 1, 15);
var rangeToPasteTo = targetSheet.getRange(targetSheet.getLastRow(),1,1,15);
var rangeToCopyFrom = sourceSheet.getRange("A50:N50");
var rangeToPasteTo = targetSheet.getRange("A4:N4");
rangeToCopyFrom.copyTo(rangeToPasteTo, {contentsOnly:true});
}
This could be us to append data to another spreadsheet:
function onFormSubmit(e) {
const dss = SpreadsheetApp.openById("id");
const sh = dss.getSheetByName("Destination Sheet Name");
sh.appendRow(e.values);
}
It needs to go into DGS. I'd need to know more about the date to determine if it's not duplicate data

In Google Apps Scripts, is there a way to create new tabs based on form data, then copy data into that newly created tab from multiple sheets?

I'm setting up a Google Sheet that is connected to a Form. The Form is bringing in data from unique users. When a new user completes the form, and the data is stored in the Form Responses 1 tab, I want a new tab to be created that is named with the name of the new user. Additionally, I want to copy in some data to this newly created tab from two other, separate tabs... one row from the Form Responses 1 tab, and a large range of data and formulas from another tab (right now I have that tab named "Sheet 5").
I'm able to create a new tab and name it based on the unique user that shows up in the Form Responses 1 tab. However, I don't know how to copy the data from those other tabs into the newly created tab because I'm trying to use getSheetbyName, which requires me to provide a named sheet. Well, I don't have a named sheet because the target sheet to be copied into will always be based on new data that comes into the form.
Any help is appreciated. Thanks.
function onFormSubmit(){
Logger.log('submit ran');
var form = FormApp.openById('1oaGxmsd8SEDJ9HrXixpriCeKYrRxr1ZVX0x1zbohTIQ');
ScriptApp.newTrigger('onFormSubmit')
.forForm(form)
.onFormSubmit()
.create();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Responses 1');
//Get last row of data
var lastRow = sheet.getLastRow();
var colB_Data = sheet.getRange(lastRow, 2).getValue();
//var thisUser = 'theUserName';
ss.insertSheet(colB_Data);
};
function copyTo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet5");
var pasteSheet = ss.getSheetByName("John Doe");
var source = copySheet.getRange(1,1,4,2);
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,4,2);
source.copyTo(destination);
The onFormSubmit function works, but the rest of the code is not working. I can't get data copied into a sheet that doesn't exist yet. It seems like I'd need the "John Doe" to be a variable that copies into each new sheet that is created
Try this:
function onFormSubmit(e){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet5');
var rg=sh.getRange(1,1,4,2);
var vA=rg.getValues();
var username=e.values[1];
var nsh=ss.insertSheet(username);
nsh.getRange(1,1,vA.length,vA[0].length).setValues(vA);
}
This code is written using the Spreadsheet Script Editor. And you have to create a onformSubmit trigger in the same project for the Spreadsheet.
onFormSubmit Trigger for Spreadsheets

Save data from one Google spreadsheet to another to create a database

I'm new to Google App Scripts and JavaScript as I work on the marketing side. I'm currently trying to create a database in Google Sheets to use in DataStudio as I want to blend data from various sources.
I have Google Sheets with a sheet to feed the data (through add-ons) and another sheet which I intend to use as the database. What I'm trying to do is to save data in the database, new data being saved at the end of the spreadsheet each time (I will set a time-based trigger when my script works). I've written a script using Google's documentation but it's only copying the first line of my Data sheet to the database. I would need it to copy all the content from the data sheet to the database sheet.
Here is the script I have for now :
// function to save data
function saveData() {
// starts with active sheet for data entry
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Data");
// collects values in data entry row
var url = sheet.getRange("Data!A1:A").getValues();
var follower_count = sheet.getRange("Data!B1:B").getValues();
var date = sheet.getRange("Data!C1:C").getValues();
// identifies and adds data to Database
var data1 = ss.getSheetByName("Database");
var aVals = data1.getRange("A1:A").getValues();
var aLastRow = aVals.filter(String).length;
Logger.log(aLastRow);
var newAttData = [[url,follower_count,date]];
Logger.log(newAttData);
data1.getRange(aLastRow +1,1,1,3).setValues(newAttData);
}
I've tried to change the range width and length in the last line but I always get an error:
Incorrect range height, was 1 but should be 3 (line 31, file "Code")
I've spent a lot of time on this but I can't get it to work. Any help would be great !
As Described in the documentation:
setValues(values)
Sets a rectangular grid of values (must match dimensions of this range).
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// The size of the two-dimensional array must match the size of the range.
var values = [
[ "2.000", "1,000,000", "$2.99" ]
];
var range = sheet.getRange("B2:D2");
range.setValues(values);
Reference

Google App Script:How can I get the Sheet name from ranges

I am new to google app script. I am stuck in a situation. I have some ranges(created in spreadsheet) and I want to get the name of sheet associated to that ranges. The following function is on docs and I want to copy data by giving some ranges from sheet.
function selectTable(sheetURL,ranges,rangevalues )
{
var workbook = SpreadsheetApp.openByUrl(sheetURL);
var currentSheetId=workbook.getId();
// get the Spreadsheet by sheet id
var totalSheets = SpreadsheetApp.openById(currentSheetId);
// select the sheet
var sourcesheet = source.getSheetByName(rangevalues);//here i ma stuck I have ranges only and I want sheetname
var abc=sourcesheet.getNamedRanges();
Logger.log("source sheet name-:"+sourcesheet);
// get the values on selectd range
var srcData = sourcesheet.getRange(ranges).getValues();
Logger.log("getting the values-:"+srcData);
}
I am referring this answer
Copy a range of SpreadSheet to a Doc.
To get the name of a sheet from a range object use:
range.getSheet().getName()

Copy a range in active sheet, create new spreadsheet with custom name, paste range

I recently wrote my first Google Apps script that makes a copy of a spreadsheet (including all tabs within that spreadsheet) and places it into a specific folder in the user's Drive. The copy is renamed based on a cell value in the original spreadsheet. Here is my script, for reference:
function copyDocument() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get current active spreadsheet.
var id = ss.getId(); // Get current active spreadsheet ID.
var sstocopy = DriveApp.getFileById(id); // Get spreadsheet with DriveApp.
var sheet = ss.getActiveSheet(); // Get current active sheet.
var sheet_name = sheet.getRange("B1").getValue(); // Get the value of cell B1, used to name the new spreadsheet.
var folder_name = sheet.getRange("C23").getValue(); // Get the target folder ID.
var folder = DriveApp.getFolderById(folder_name); // Get the ID of the folder where you will place a copy of the spreadsheet.
sstocopy.makeCopy(sheet_name,folder); // Make a copy of the spreadsheet in the destination folder.
}
This script works, but I have been asked to modify it because the script I wrote is copying over unnecessary tabs and data that is causing confusion to users.
The new script should make a copy of a specific range in a specific sheet, create a new spreadsheet, and paste that range into it. It should also name itself after a cell value in the range.
However, the only method I have come across that specifically copies a sheet into a new spreadsheet is copyTo(spreadsheet). However, the Google Apps Script Guide specifies that "the copied sheet will be named 'Copy of [original name]'" by default.
I want to be able to rename the copied sheet after a specific cell. My question is, can I use copyTo(spreadsheet) and give the new spreadsheet a custom name, based on a cell?
Thanks!
You will need to get the data from the specific sheet to copy out to a variable of using:
var sourceSheet = ss.getSheetByName("Sheet1");
var sourceData = sourceSheet.getDataRange().getValues();
var originalRangeNotation = sourceSheet.getDataRange().getA1Notation();
Then you need to create a new, empty file and make a sheet with the same name
var ssNew = SpreadsheetApp.create("My New File Name");
ssNew.insertSheet('My New Sheet');
Then add the contents from the saved data to the new file. Since the insertSheet makes the new sheet the active one, we use:
var sheetNew = ssNew.getActiveSheet();
var rangeNew = sheet.getRange(originalRangeNotation);
range.setValues(sourceData);
Thanks. I ended up using this in place of sstocopy.makeCopy()
folder.addFile()
new_sheet.getActiveSheet().getRange("X:Y").setValues(sheet.getRange("X:Y").getValues())
DriveApp.getRootFolder().removeFile(temp)