Я создаю веб-приложение Google, чтобы открыть лист Google, к которому я добавил строку, а затем поместить курсор в ячейку, в которую скрипт добавил новый текст.Все работает, но последний шаг - размещение курсора.
Код следует:
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function addSong(objArgs) {
var ss = SpreadsheetApp.openById('id');
var url = ss.getUrl();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = objArgs.songTitle;
Logger.log('songTitle: ' + songTitle)
var namedRange = sheet.getRange("Title");
var newRange = sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
newRange.setValue(songTitle);
SpreadsheetApp.setActiveSpreadsheet(ss);
SpreadsheetApp.setActiveSheet(sheet);
SpreadsheetApp.setActiveRange(newRange);
//I suspect the error is in the lines below
return {
url: url,
newRange: newRange
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<br>Fill in field below with Song Title.<br>
Then click button to add new song to Catalog.<br><br>
<input id="idSongTitle" type="text" placeholder="Song Title"><br><br>
<button onclick="saveUserInput()">Add song to Catalog</button>
<script>
window.saveUserInput = function() {
var songTitle = document.getElementById('idSongTitle').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openCatalog)
.addSong({songTitle:songTitle})
}
// This function is detritus left over from something else. Doesn't seem to be impacting the rest of this.
function openCatalog(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
Пересмотренный код:
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function addSong(objArgs) {
var id= "1-yN6yAGUuOp84apeN2Cwaq25rPD63qb_m2Oe-MSMaIM"
var ss = SpreadsheetApp.openById(id);
var url = ss.getUrl();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = objArgs.songTitle;
Logger.log('songTitle: ' + songTitle)
var namedRange = sheet.getRange("Title");
var newRange = sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
newRange.setValue(songTitle);
Logger.log(url)
return {
url: url
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<br>Fill in field below with Song Title.<br>
Then click button to add new song to Catalog.<br><br>
<input id="idSongTitle" type="text" placeholder="Song Title"><br><br>
<button onclick="saveUserInput()">Add song to Catalog</button>
<script>
window.saveUserInput = function() {
var songTitle = document.getElementById('idSongTitle').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openCatalog)
.addSong({songTitle:songTitle})
}
function openCatalog(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
Окончательный сценарий
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function addSong(objArgs) {
var id= "1-yN6yAGUuOp84apeN2Cwaq25rPD63qb_m2Oe-MSMaIM"
var ss = SpreadsheetApp.openById(id);
var url = ss.getUrl();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = objArgs.songTitle;
var namedRange = sheet.getRange("Title");
var newRange = sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
var newLastRow = lastRow+1
newRange.setValue(songTitle);
url = url + "#gid=1286827341" + "&range=" + newLastRow + ":" + newLastRow;
Logger.log('url: ' + url)
return {
url: url
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<br>Fill in field below with Song Title.<br>
Then click button to add new song to Catalog.<br><br>
<input id="idSongTitle" type="text" placeholder="Song Title"><br><br>
<button onclick="saveUserInput()">Add song to Catalog</button>
<script>
window.saveUserInput = function() {
var songTitle = document.getElementById('idSongTitle').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openCatalog)
.addSong({songTitle:songTitle})
}
function openCatalog(url){
window.open(url);
}
</script>
</body>
</html>
Небольшое редактирование
Я отключился ...
return {
url: url
};
для ...
return url;
потому что первый выдавал 400 (неверный запрос) ошибок.