【问题标题】:Using Script to add an ='Sheet1!'D3 formula that has a sheet name that is pulled within the script使用脚本添加 ='Sheet1!'D3 公式,该公式具有在脚本中提取的工作表名称
【发布时间】:2018-07-27 14:23:39
【问题描述】:

我有这个:

var app = SpreadsheetApp;
var activesheet = app.getActiveSpreadsheet().getActiveSheet();
var RecipeList = app.getActiveSpreadsheet().getSheetByName('Recipe List')

var recipename = activesheet.getRange('D3').getValue();
var costperserve = activesheet.getRange('J5').getValue();
var foodcostpercentage = activesheet.getRange('J6').getValue();
var sellingprice = activesheet.getRange('J7').getValue();
var grossprofit = activesheet.getRange('J10').getValue();
var spreadsheetname = activesheet.getSheetName() ;
var Last = RecipeList.getLastRow();

RecipeList.getRange(Last+1,1,1,1).setValue(recipename), {contentsOnly:true};
RecipeList.getRange(Last+1,2,1,1).setValue(costperserve), {contentsOnly:true};  
RecipeList.getRange(Last+1,3,1,1).setValue(foodcostpercentage), 
{contentsOnly:true};  
RecipeList.getRange(Last+1,4,1,1).setValue(sellingprice), {contentsOnly:true};
RecipeList.getRange(Last+1,5,1,1).setValue(grossprofit), {contentsOnly:true};
RecipeList.getRange(Last+1,6,1,1).setValue(spreadsheetname), 
{contentsOnly:true};

虽然我希望 = ( = 'spreadsheetname' !D3) 的值只是该值的一个姿势,但我可以更改该值并对其进行更新。

【问题讨论】:

标签: google-sheets formulas


【解决方案1】:

有一个setFormula() 方法可以像setValue() 一样使用,但这不是必需的。您也可以在这里使用setValue()

您似乎正试图将{contentsOnly:true} 的选项传递给setValue(),但这是无效的。正如您在链接文档中看到的那样,此方法不允许任何选项。此外,它不允许该特定选项,因为无论如何它只会影响内容。

试试这个示例代码:

function insertFormulaTest() {
  var ss = SpreadsheetApp.getActive();
  var sourceSheet = ss.getSheetByName("Source Sheet");
  var sourceCell = sourceSheet.getRange("D3");
  var formula = "='" + sourceSheet.getName() + "'!" + sourceCell.getA1Notation(); // "='Source Sheet'!D3"
  var destinationSheet = ss.getSheetByName("Destination Sheet");
  destinationSheet.getRange("A1").setValue(formula);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多