我在采購請求電子表格中有一個自定義腳本。它生成新選項卡并將新選項卡中的鏈接添加到“注冊”(第一個選項卡)以匯總資料。
不幸的是,選項卡名稱通常以浮點格式 (206.0) 而不是整數格式生成,因此始終為整數格式的選項卡名稱參考不起作用。我無法弄清楚為什么會出現浮動。它不會發生在我們嘗試的每臺計算機上,只會發生在大多數計算機上,這使得它更加神秘。
我嘗試了不同的方法將其強制為整數,但沒有找到解決方案。非常歡迎所有建議。
function newPR(e) {
// First, set up the variables we'll need
var ui = SpreadsheetApp.getUi()
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheet = sheets[0];
var templateSheet = sheets[1];
var highestPurchaseRequest = sheets[3];
var today = new Date();
var requestCount = sheet.getRange(6,1).getValue(); //getRange: row, column
var lastRequestTab = parseInt(highestPurchaseRequest.getName(),10);
var checkNeeded = ( requestCount != lastRequestTab );
var loopCount = 0;
/* Before starting, check whether the highest request number matches the
name of the highest estimate tab created so far. If not, try to fix it a few
times. If you can't fix it automatically, flag an error and ask the user to
sort it out.
*/
if( checkNeeded ) {
while( loopCount < 100 && checkNeeded ) {
if( requestCount <= lastRequestTab ) {
requestCount ;
} else {
checkNeeded = false;
}
loopCount
}
if( checkNeeded ){
ui.alert("Something went wrong!",'The highest Purchase Request listed on the Summary tab is lower than leftmost Purchase Request tab in the spreadsheet.\n\nI\'ve tried to fix this automatically but haven\'t been able to. Maybe someone dragged tabs into a different order accidentally? The order should be: Register, TemplatePO, TemplateCS, Highest-numbered-request.\n\nPlease correct it and retry. ',ui.ButtonSet.OK);
return;
}
}
/* Everything looks good, so create a new sheet using "templatePO" as the template,
increment the request number and use it to name the new sheet; fill in the
current date & estimate number at the top
*/
requestCount = requestCount >> 0;
ss.insertSheet(requestCount.toString(), 3, {template: templateSheet});
// Now that it's made, we need another variable to store a reference to the new sheet
var newSheet = SpreadsheetApp.getActiveSheet();
newSheet.getRange(5,9).setValue( today );
// Switch to the summary sheet and add links to the new data
ss.setActiveSheet( sheets[0] );
ss.insertRowBefore(6);
var newCells = sheet.getRange(6,1,1,9);
// Data from: PR# in I2, Date in I5, From in D4, For/Description in D5, Total in D7, sent in K2, received in K11, Notes in K5
var requestData = [ [ requestCount, "='" requestCount "'!I5", "='" requestCount "'!D4", "='" requestCount "'!D5", "='" requestCount "'!K2", "='" requestCount "'!K11", "='" requestCount "'!D7","",""] ]
newCells.setFormulas( requestData );
// Switch back to the new sheet & get ready for user input
ss.setActiveSheet( newSheet );
newSheet.setActiveSelection("D5");
}
uj5u.com熱心網友回復:
要將數字轉換為沒有小數部分的字串,您可以使用toFixed(0).
嘗試改變
ss.insertSheet(requestCount.toString(), 3, {template: templateSheet});
到
ss.insertSheet(requestCount.toFixed(0), 3, {template: templateSheet});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/479571.html
標籤:javascript 谷歌应用脚本 谷歌表格 类型
上一篇:自定義日期和時間格式
