I have multiple sheets in the spreadsheets and the code I have works only when the sheet is on first and but on other positions
var doc = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetName = '管理_201907_CUD';
var sheet = doc.getSheetByName(sheetName);
if i were to run the function solely it would work
function test(){
SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName('管理_201907_CUD').getRange('A1')
.setValue('test');
}
but then again the data i have is coming from a Reactjs web app using XHR
it works but only when the sheet is on the first place and not on other positions
Normally it should be var sheetName = e.parameters.formGoogleSheetName;
e.parameters.formGoogleSheetName takes it value from the web app but for now I directly inputted the sheetname
i just want my sheet to work even though its on a different place
from this
to this
Related
I am new to API and google script, so please bare with me.
How do i fetch a CSV file through an API in the right format.
Through UrlFetchApp i was able to fetch the data, but in the wrong format, since the data is not seperated into cells.
function callNumbers() {
var response = UrlFetchApp.fetch("https://api.statbank.dk/v1/data/VG2/CSV?valuePresentation=Default&allowVariablesInHead=true&K%C3%98RART=1000&M%C3%86NGDE4=0070&Tid=2020K3%2C2020K2%2C2020K1");
var data = response.getContentText()
Logger.log(data);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1).setValue([data]);
}
How do i separate the data into rows (defined by line breaks) and column (defined by the semicolon)?
Please help I am so Lost!
I need to move a shortcut file from one destination to another using google apps script.
Usually I would move a file or folder like this:
function move(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sStamdata = ss.getSheetByName("New");
var folderOld = DriveApp.getFolderById(folderOldId);
var destination = DriveApp.getFolderById(folderId);
var id = sStamdata.getRange('D77').getValue();
var file = DriveApp.getFileById(id);
folderOld.removeFile(file)
destination.addFile(file)
But this doesn't work with shortcuts. Any ideas?
How about this answer?
Modification points:
It seems that in the current stage, the shortcut cannot be moved using Drive service. I think that this might be resolve in the future update.
So as the current workaround, in this answer, I would like to propose to use the method of "Files: patch" Drive API v2, because Drive API of Advanced Google services is used.
When your script is modified, please modify as follows.
Modified script:
Before you run the script, please enable Drive API at Advanced Google services.
function move(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sStamdata = ss.getSheetByName("New");
// var folderOld = DriveApp.getFolderById(folderOldId);
// var destination = DriveApp.getFolderById(folderId);
var id = sStamdata.getRange('D77').getValue();
// var file = DriveApp.getFileById(id);
Drive.Files.patch({parents: [{id: folderId}]}, id); // Added
}
folderId is the destination folder ID.
Please confirm the file ID of the shortcut again.
Note:
For example, when the file ID of the shortcut is confirmed with the shared link using the browser, the file ID is ID of the original file. Please be careful this.
References:
Advanced Google services
Files: patch
I've been trying to figure out why part of my Google App script doesn't work, but I've failed to come up with an answer.
The script is downloading an attachment, CSV, from an email in Gmail and stores in with a specific name in a specific folder - this works perfectly fine.
But then I want to edit the CSV, and this is where I run into problems.
var newFolderIterator = DriveApp.getFoldersByName(destinationFolderName)
var newFolderId, myFileName, myFileId
while(newFolderIterator.hasNext()) {
newFolderId = newFolderIterator.next().getId()
var newFileList = DriveApp.getFolderById(newFolderId).getFiles()
while(newFileList.hasNext()) {
myFileName = newFileList.next()
myFileId = myFileName.getId()
var myFileURL = myFileName.getUrl()
Logger.log(myFileName.getId() + " " + myFileName.getName()) //Logs the ID and Name
Logger.log(myFileURL) //Logs the URL
var ss = SpreadsheetApp.openById(myFileName.getId()) //Error, cannot find the ID (error message: perhaps it's missing?)
}
}
I've tried using the openByURL as well, with the same error message.
Probably really easy to fix, any hints and tips is appreciated.
Thanks
The problem here is you are uploading a CSV but attempting to open it with SpreadsheetApp. SpreadsheetApp can only open Google Sheets documents, and your CSV is not automatically converted.
The CSV must first be converted to a Google Sheets document before you can access it with SpreadsheetApp.
You may be able to do this on upload, but I haven't tried this personally. This question looks relevant:
How to automatically import data from uploaded CSV or XLS file into Google Sheets
I want to send notification to third party application when someone make changes in document stored in google drive.
can someone please help me that how to bound script with any document and when someone make changes in that script should run and send notification to third party application.
I have tried the following code But it is not working.
function onEdit(event){
var sheet = event.source.getActiveSheet();
var editedRow = sheet.getActiveRange().getRowIndex();
var editedolumn = sheet.getActiveRange().getColumnIndex();
var values = sheet.getSheetValues(editedRow, editedolumn, 1, 6);
Logger.log(values);
getSession();
}
function getSession(){
var payload =
{
"username" : "username",
"password" : "password",
};
var options =
{
"method" : "post",
"payload" : payload,
"followRedirects" : false
};
var login = UrlFetchApp.fetch("https://abcd.service-now.com/nav_to.do?uri=login.do" , options);
Logger.log(login);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails);
sendHttpPost(sessionDetails);
}
function sendHttpPost(data) {
var payload = {"category" : "network","short_description" : "Test"};
var headers = {"Cookie" : data}
var url = 'https://abcd.service-now.com/api/now/table/incident';
var options = {'method': 'post','headers': headers,'payload': payload,'json': true};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
To send notification to third party application when someone make changes in document stored in google drive
Based from this Google Drive Help Forum, this feature hasn't been added yet. However, you may set notifications in a spreadsheet to find out when there's some modifications done in your spreadsheet. To set notifications in a spreadsheet:
Open the spreadsheet where you want to set notifications.
Click Tools > Notification rules.
In the window that appears, select when and how often you want to
receive notifications.
Click Save.
And, to bound script with any document
You may find the complete guide in Scripts Bound to Google Sheets, Docs, or Forms documentation. As mentioned,
To create a bound script, open a Google Sheets, Docs, or Forms file, then select Tools > Script editor. To reopen the script in the future, do the same thing. Because bound scripts do not appear in Google Drive, that menu is the only way to find or open the script.
I have an HTML page that produces data (a table row). I want to store rows from all clients in an online table which can be accessed/downloaded (preferably by the owner alone, so anonymous clients can only add rows)
Possible solutions and encountered problems:
Google spreadsheet + google apps script - How to do a cross origin POST request?
Google fusion tables - How to add rows from anonymous clients? Is that possible?
Google app engine - possible, but seems too time consuming for this simple task.
I've found an answer on how to do cross domain POST, so I've managed to do what I want with apps script + spreadsheet:
Apps script:
function doPost(request) {
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheets()[0];
if (request.parameters.row != null) {
sheet.appendRow(Utilities.jsonParse(request.parameters.row));
}
}
Client javascript (from https://stackoverflow.com/a/6169703/378594):
function crossDomainPost(paramsDict, url) {
// Add the iframe with a unique name
var iframe = document.createElement("iframe");
var uniqueString = "SOME_UNIQUE_STRING";
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.contentWindow.name = uniqueString;
// construct a form with hidden inputs, targeting the iframe
var form = document.createElement("form");
form.target = uniqueString;
form.action = url;
form.method = "POST";
// repeat for each parameter
for (i in paramsDict) {
var input = document.createElement("input");
input.type = "hidden";
input.name = i;
input.value = paramsDict[i];
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
}
crossDomainPost({'row': JSON.stringify([123, 123, 1211])}, serverURL);
Note that a similar scheme should work for Google fusion tables.